Author: Ma Ning
Sample Code:
Wp7_aawolf_ironrubywp7.rar
I wrote this blog because I read this article on msdn:
Http://msdn.microsoft.com/en-us/magazine/ff960707.aspx
Windows Phone 7 development tools do not support dynamic languages, so ironruby's support for Windows Phone 7 is particularly important. When I first read this article, I made a joke and read the code again. I don't know a single sentence. I wonder if the language is switched to. net. Why is the change so big? Taking a closer look, it turned out to be Ruby, not Python ^_^. This time the python fell behind. I used to write automated test scripts in Python, but I have never touched Ruby. So I think of Ruby as Python.
Dynamic Language is not supported. It has always been a pain point in Windows Mobile programming. Finally, the logic of the program can be changed dynamically, which gives us unlimited imagination. On Windows Phone, is F # Faster? Pai_^
Let's get down to the truth. This time, I just wrote some key points in my attempt to minimize detours. For more information, please refer to Shay Friedman's blog: http://ironshay.com/
First, download ironruby for Windows Phone (. NET 3.5 ):
Http://ironruby.codeplex.com/releases/view/43540#DownloadId=133276
Then, create a Silverlight for Windows Phone 7 project in Visual Studio 2010 named "ironrubywp7" and select the "add reference" option in the "project" menu, in the pop-up dialog box, select the "Browse" label. We can find the decompressed ironruby directory and add the DLL files in/Silverlight/bin to the project:
After some warning prompts are ignored, the Assembly will be added to the project. We can see the newly added assembly in Solution Explorer:
Next, add a text file to the project, select ironrubywp7 in Solution Explorer, right-click "add"-"new item", and select "text file" in the dialog box ", change the file name to "mainpage. RB ".
Select the mainpage. RB file and set "build action" to "embedded resource" in the property ".
Open the mainpage. RB file and enter the following Ruby code:
# Include namespaces for ease of useinclude System::Windows::Mediainclude System::Windows::Controls# Set the titlesPhone.find_name("ApplicationTitle").text = "aawolf.cnblogs.com"Phone.find_name("PageTitle").text = "IronRuby& WP7"# Create a new text blocktextBlock = TextBlock.newtextBlock.text = "IronRuby is running on Windows Phone 7!"textBlock.foreground = SolidColorBrush.new(Colors.Green)textBlock.font_size = 48textBlock.text_wrapping = System::Windows::TextWrapping.Wrap# Add the text block to the pagePhone.find_name("ContentPanel").children.add(textBlock)
Note that I have modified the name of the Container Control in the last line. The name in the original example is "contentgrid ", however, by default, the container class name in the XAML File Created by the Silverlight for Windows Phone wizard is "contentpanel ". This may cause a runtime error because ironruby cannot debug, so the first debugging is quite painful.
Next, open the mainpage. XAML. CS file, and add the ironruby initialization code to the constructor of the mainpage class:
public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); // Allow both portrait and landscape orientations SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape; // Create an IronRuby engine and prevent compilation ScriptEngine engine = Ruby.CreateEngine(); // Load the System.Windows.Media assembly to the IronRuby context engine.Runtime.LoadAssembly(typeof(Color).Assembly); // Add a global constant named Phone, which will allow access to this class engine.Runtime.Globals.SetVariable("Phone", this); // Read the IronRuby code Assembly execAssembly = Assembly.GetExecutingAssembly(); Stream codeFile = execAssembly.GetManifestResourceStream("IronRubyWP7.MainPage.rb"); string code = new StreamReader(codeFile).ReadToEnd(); // Execute the IronRuby code engine.Execute(code); } }
Please pay attention to the position of the initializecomponent method. Before initializing ironruby runtime, you must initialize the control first. This is my lesson. Another important thing to note is to read the path of the Ruby file. We can also get a stream through httprequest, right? Laugh and leave it blank ^_^
The final running effect is as follows: the entire development process lasted two hours:
Related Resources
Ma Ning's Windows Phone 7 Development tutorial (1) -- Windows Phone development tool's initial experience
Windows Phone 7 Development tutorial (2)-Windows Phone xNa 4.0 3D Game Development
Windows Phone 7 Development tutorial (3)-use MessageBox and soft keyboard in xNa
Ma Ning's Windows Phone 7 Development tutorial (4) -- xNa displays Chinese Fonts