appcelerator/titanium_mobile — pull request #5361 by bhatfield (2014-02-20 01:18:20+00:00)
Title: [TIDOC-1487] Implement APIs to create Native Windows Phone components

Initial work was started by Bryan. Completed most of the APIs he exposed and added a few others to make it easier to retrieve some of the underlining components.

My *lovely* test module:
~~~

exports.init = function () {

var appPage = Ti.MobileWeb.WP8.getPhoneApplicationPage();
var grid = appPage.getProp('Content');

// Removes any elements from the current view
var children = grid.getProp('Children');
var count = children.getProp('Count');
children.invoke('Clear', [], []);

var height = grid.getProp('ActualHeight');
var width = grid.getProp('ActualWidth');

// Create a canvas that takes up the entire screen
canvas = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.Canvas', [], []);
canvas.setProp('Height', height);
canvas.setProp('Width', width);

// Get the blue enum, create a solid color brush and set the canvas' background color
var blue = canvas.getEnum('System.Windows.Media.Colors', 'Blue')
var solidblue = Ti.MobileWeb.WP8.createInstance('System.Windows.Media.SolidColorBrush', ['System.Windows.Media.Color'], [blue]);
var orange = canvas.getEnum('System.Windows.Media.Colors', 'Orange')
var solidorange = Ti.MobileWeb.WP8.createInstance('System.Windows.Media.SolidColorBrush', ['System.Windows.Media.Color'], [orange]);
canvas.setProp('Background', solidblue);

// Add the canvas to the root view
children.invoke('Add', ['System.Windows.Controls.Canvas'], [canvas]);

var button = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.Button', [], []);
button.setProp('Content', 'Click me, I\'m Native');

button.setProp('Foreground', solidorange);
button.setProp('BorderBrush', solidorange);

var child = canvas.getProp('Children');
canvas.invoke('SetTop', ['System.Windows.Controls.Button', 'System.Double'], [button, (height / 2)]);
canvas.invoke('SetLeft', ['System.Windows.Controls.Button', 'System.Double'], [button, (width / 4)]);
child.invoke('Add', ['System.Windows.Controls.Button'], [button]);

var button1 = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.Button', [], []);
var bitmap1 = Ti.MobileWeb.WP8.createInstance('System.Windows.Media.Imaging.BitmapImage', ['System.Uri'], ["/Icons/add.png"]);
var image1 = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.Image', [], []);
image1.setProp('Source', bitmap1);
image1.setProp('Height', 50);
image1.setProp('Width', 50);
button1.setProp('Content', image1);

var button2 = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.Button', [], []);
var bitmap2 = Ti.MobileWeb.WP8.createInstance('System.Windows.Media.Imaging.BitmapImage', ['System.Uri'], ["/Icons/close.png"]);
var image2 = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.Image', [], []);
image2.setProp('Source', bitmap2);
image2.setProp('Height', 50);
image2.setProp('Width', 50);
button2.setProp('Content', image2);

var stackPanel = Ti.MobileWeb.WP8.createInstance('System.Windows.Controls.StackPanel', [], []);
var horizontalOrientation = stackPanel.getEnum('System.Windows.Controls.Orientation', 'Horizontal');
stackPanel.setProp('Orientation', horizontalOrientation);
var stackPanelChildren = stackPanel.getProp('Children');
stackPanelChildren.invoke('Add', ['System.Windows.Controls.Button'], [button1]);
stackPanelChildren.invoke('Add', ['System.Windows.Controls.Button'], [button2]);
child.invoke('Add', ['System.Windows.Controls.StackPanel'], [stackPanel]);

button1.addEventListener('Click', function (e) {
// Opens a default Phone Application Page (created in Visual Studio) and adds it to the nav stack
var appPage = Ti.MobileWeb.WP8.getPhoneApplicationPage();
var navService = appPage.getProp('NavigationService');
navService.invoke('Navigate', ['System.Uri'], ["/App/Page1.xaml"]);
});

button2.addEventListener('Click', function (e) {

// Remove native controls and readd the browser
var appPage = Ti.MobileWeb.WP8.getPhoneApplicationPage();
var grid = appPage.getProp('Content');
var children = grid.getProp('Children');
children.invoke('Clear', [], []);
var browser = Ti.MobileWeb.WP8.getWebBrowser();
children.invoke('Add', ['Microsoft.Phone.Controls.WebBrowser'], [browser]);

// Clean up
var handles = Ti.MobileWeb.WP8.getProxyList();
for (var key in handles) {
if (key == 'browser' || key == 'app') continue;
handles[key].destroy();
}

});

button.addEventListener('Click', callback);

function callback(e) {
e.source.setProp("Content", "Stop clicking me!");
e.source.removeEventListener('Click', callback);
};

return;
}
~~~