Windows Phone7中的IronRuby

來源:互聯網
上載者:User

作者:馬寧

範例程式碼:

WP7_aawolf_IronRubyWP7.rar

寫這篇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檔案加入到工程中來:


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


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

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

我們開啟MainPage.rb檔案,輸入下面的Ruby代碼:

 

# 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)

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

接下來,我們要開啟MainPage.xaml.cs檔案,將IronRuby初始化代碼,加入到MainPage類的建構函式中:

    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);        }    }

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

最後啟動並執行效果是這樣子的,整個開發過程曆時兩小時:

相關資源

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

馬寧的Windows Phone 7開發教程(2)——Windows Phone XNA 4.0 3D遊戲開發

馬寧的Windows Phone 7開發教程(3)——XNA下使用MessageBox和軟鍵盤

馬寧的Windows Phone 7開發教程(4)——XNA顯示中文字型

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.