天天看點

Windows Phone7中的IronRuby

作者:馬甯

示例代碼下載下傳見附件。

寫這篇BLOG完全是因為看了MSDN上的這篇文章:

http://msdn.microsoft.com/en-us/magazine/ff960707.aspx

    Windows Phone 7的開發工具不支援動态語言,是以IronRuby支援Windows Phone 7就顯得格外重要了。剛看這篇文章的時候,還鬧了個笑話,看了一遍代碼,一句都不認識,心想難道語言換到.NET上,變化怎麼這麼大?仔細一看,原來是Ruby,而不是Python ^_^,小蟒蛇這次落後了。以前用Python寫過自動化測試腳本,沒接觸過Ruby,是以,把Ruby看成Python了。

    不支援動态語言,一直是Windows Mobile程式設計的痛,這次終于有搞頭了。終于可以動态改變程式的邏輯了,光這一點就給我們提供了無限的想象空間。Windows Phone上的F#也快了吧?^_^

    言歸正傳,這次我完全是照葫蘆畫瓢,隻是将自己嘗試中的一些關鍵點寫出來,讓大家少走彎路。更多資訊大家可以參考Shay Friedman的BLOG:http://ironshay.com/

    首先,我們要下載下傳IronRuby for Windows Phone版本(.NET 3.5):

http://ironruby.codeplex.com/releases/view/43540#DownloadId=133276

    然後,在Visual Studio 2010中建立一個Silverlight for Windows Phone 7的工程,工程名叫做“IronRubyWP7”,然後選擇“Project”菜單的“Add Reference”選項,在彈出的對話框中,選擇“Browse”标簽,我們可以找到解壓後的IronRuby目錄,将\silverlight\bin中的DLL檔案加入到工程中來:

Windows Phone7中的IronRuby
    在忽略一些警告提示之後,程式集将被加入到工程中。我們可以在Solution Explorer中看到剛被加入的程式集:
Windows Phone7中的IronRuby

    接下來,我們在工程中添加一個文本檔案,在Solution Explorer中選中IronRubyWP7,右鍵,“Add”-“New Item”,在對話框中選擇“Text File”,将檔案名改為“MainPage.rb”。

    選中MainPage.rb檔案,在屬性中将“Build Action”設定為“Embedded Resource”。

Windows Phone7中的IronRuby
    我們打開MainPage.rb檔案,輸入下面的Ruby代碼:

  1. # Include namespaces for ease of use  
  2. include System::Windows::Media  
  3. include System::Windows::Controls  
  4. # Set the titles  
  5. Phone.find_name("ApplicationTitle").text = "aawolf.cnblogs.com" 
  6. Phone.find_name("PageTitle").text = "IronRuby& WP7" 
  7. # Create a new text block  
  8. textBlock = TextBlock.new 
  9. textBlock.text = "IronRuby is running on Windows Phone 7!" 
  10. textBlock.foreground = SolidColorBrush.new(Colors.Green)  
  11. textBlock.font_size = 48  
  12. textBlock.text_wrapping = System::Windows::TextWrapping.Wrap  
  13. # Add the text block to the page  
  14. Phone.find_name("ContentPanel").children.add(textBlock) 

    請注意,我修改了最後一行的容器控件名稱,原示例中的名稱是“ContentGrid”,但是Silverlight for Windows Phone向導預設建立的XAML檔案中容器類名稱是“ContentPanel”。這裡會引起一個運作期錯誤,因為IronRuby不能Debug,是以第一次調試起來比較痛苦。

    接下來,我們要打開MainPage.xaml.cs檔案,将IronRuby初始化代碼,加入到MainPage類的構造函數中:

  1. public partial class MainPage : PhoneApplicationPage  
  2.     {  
  3.         // Constructor  
  4.         public MainPage()  
  5.         {  
  6.             InitializeComponent();  
  7.             // Allow both portrait and landscape orientations  
  8.             SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;  
  9.             // Create an IronRuby engine and prevent compilation  
  10.             ScriptEngine engine = Ruby.CreateEngine();  
  11.             // Load the System.Windows.Media assembly to the IronRuby context  
  12.             engine.Runtime.LoadAssembly(typeof(Color).Assembly);  
  13.             // Add a global constant named Phone, which will allow access to this class 
  14.             engine.Runtime.Globals.SetVariable("Phone", this);  
  15.             // Read the IronRuby code  
  16.             Assembly execAssembly = Assembly.GetExecutingAssembly();  
  17.             Stream codeFile =  
  18.               execAssembly.GetManifestResourceStream("IronRubyWP7.MainPage.rb");  
  19.             string code = new StreamReader(codeFile).ReadToEnd();  
  20.             // Execute the IronRuby code  
  21.             engine.Execute(code);  
  22.         }  
  23.     } 

    請大家注意InitializeComponent方法的位置,在初始化IronRuby運作時之前,一定要先完成控件初始化的工作,這是我血和淚的教訓。另外一個需要注意的地方,就是讀取Ruby檔案的路徑。我們似乎也可以通過HttpRequest擷取一個Stream是吧?笑而不語 ^_^

    最後運作的效果是這樣子的,整個開發過程曆時兩小時:

Windows Phone7中的IronRuby

相關資源

馬甯的Windows Phone 7開發教程(1)——Windows Phone開發工具初體驗

繼續閱讀