自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用IronRuby開發(fā)Windows Phone 7應用程序

原創(chuàng)
移動開發(fā)
本文我們將為大家講解如何使用IronRuby開發(fā)Windows Phone 7應用程序,由于筆者最近在研究IronRuby,因此也想嘗試一下用IronRuby開發(fā)Windows Phone 7應用程序,因此有了Lifting Calculator這款程序。

【51CTO譯文】微軟發(fā)布Windows Phone 7平臺后,Windows Phone發(fā)展并不是非常迅速,但是諾基亞牽手微軟后使更多開發(fā)者看到了Windows Phone 7平臺的潛力,51CTO也歇盡全力為不同語言的開發(fā)者打造一個開發(fā)Windows Phone應用的平臺。前不久51CTO給大家奉獻過《寫給Android開發(fā)者的Windows Phone開發(fā)秘籍》,本文我們將為大家講解一個外國開發(fā)者賈斯汀 詹姆斯(Justin James)如何使用IronRuby開發(fā)Windows Phone 7應用程序,在未來51CTO將為大家講述解不同語言開發(fā)Windows Phone 7應用的實例。

IronRuby

以下為全部譯文

數(shù)周前,筆者在微軟贊助的一個比賽中免費獲得了一部三星Focus手機,激起了筆者開發(fā)一個Windows Phone 7應用程序的欲望,由于筆者最近在研究IronRuby,因此也想嘗試一下用IronRuby開發(fā)Windows Phone 7應用程序,因此有了Lifting Calculator這款程序。

使用IronRuby開發(fā)Windows Phone 7應用程序

下載IronRuby

筆者的電腦上安裝了IronRuby 1.1.1,但筆者更熟悉IronRuby 1.1.2,并且它包括了Windows Phone 7支持,因此筆者下載了二進制包(MSI版本不友好)。

使用IronRuby編寫一個Windows Phone 7應用程序

筆者曾拜讀過Shay Friedman發(fā)表在MSDN上一篇題為“Windows Phone 7上的IronRuby”的文章,筆者調(diào)整了他的方向以滿足筆者的需要,一方面是因為筆者已經(jīng)完成了一部分功能,另一方面,筆者想嘗試一下如何使用IronRuby,此外,那篇文章給出的說明不是完全正確。

下面是筆者采取的步驟:

◆添加IronRuby包的引用,筆者從目錄中添加了所有Windows Phone 7二進制包。

◆準備XAML,添加需要的代碼(筆者還沒有使用MVVM)。

◆在調(diào)用Ruby的C#代碼中,筆者添加了下面的Using語句:

  1. using System.Reflection;  
  2. using System.IO;  
  3. using Microsoft.Scripting.Hosting;  
  4. using IronRuby; 

◆創(chuàng)建Ruby文件,包含需要的函數(shù),Ruby腳本應該操作通過Silverlight主機傳遞的全局變量,然后返回一個輸出值。

◆筆者將生成操作設置為將新Ruby文件復制到輸出目錄。

◆添加調(diào)用IronRuby腳本的代碼,通過一個變量接收它的輸出。

詳細信息請查看示例代碼A(調(diào)用IronRuby腳本的C#代碼)和B(IronRuby腳本)。

在Shay的文章中,他將整個應用程序傳遞給了腳本,相反,筆者只傳遞了表示輸入?yún)?shù)的對象和容納輸出的對象,運行這個腳本時,將輸出信息顯示在屏幕上。

示例代碼A:調(diào)用IronRuby腳本的C#代碼

  1. private void ShowBarbellLoadout(int barbellWeight, int desiredLoad) {   
  2. var resourceStream = Application.GetResourceStream(new Uri("BarbellLoader.rb", UriKind.Relative));   
  3. var dataFile = new StreamReader(resourceStream.Stream);   
  4. var code = dataFile.ReadToEnd();   
  5. var engine = Ruby.CreateEngine();   
  6. engine.Runtime.Globals.SetVariable("BarbellWeight", barbellWeight);   
  7. engine.Runtime.Globals.SetVariable("DesiredLoad", desiredLoad);   
  8. var loadoutResults = (IronRuby.Builtins.Hash)engine.Execute(code);   
  9. var results = new List<BarbellLoadout> { {new BarbellLoadout{ PlateSize = 45, PlateCount = int.Parse(loadoutResults["45"].ToString()) }}, {new BarbellLoadout{ PlateSize = 25, PlateCount = int.Parse(loadoutResults["25"].ToString()) }}, {new BarbellLoadout{ PlateSize = 10, PlateCount = int.Parse(loadoutResults["10"].ToString()) }}, {new BarbellLoadout{ PlateSize = 5, PlateCount = int.Parse(loadoutResults["5"].ToString()) }}, {new BarbellLoadout{ PlateSize = 2.5M, PlateCount = int.Parse(loadoutResults["2.5"].ToString()) }} };   
  10. loadingChart.ItemsSource = results;  
  11. mainPivotControl.SelectedItem = barbellLoading;  

示例代碼B:IronRuby腳本

  1. currentTotal = DesiredLoad.to_i - BarbellWeight.to_i output = {}    
  2. output["45"] = (currentTotal / 90).truncate    
  3. currentTotal -= output["45"] * 90    
  4. output["25"] = (currentTotal / 50).truncate    
  5. currentTotal -= output["25"] * 50 output["10"] = (currentTotal / 20).truncate    
  6. currentTotal -= output["10"] * 20 output["5"] = (currentTotal / 10).truncate    
  7. currentTotal -= output["5"] * 10 output["2.5"] = (currentTotal / 5).truncate    
  8. currentTotal -= output["2.5"] * 5 return output  

 

筆者學到了一個非常重要的教訓:外部全局變量設置必須使用大寫名稱,否則變量將不起作用。更令人沮喪的是,在筆者的腳本中,全局變量名稱前有一個美元符號前綴,這樣做本身并沒有錯,變量也會初始化,但卻沒有輸出,筆者在這個上面花了大量時間才解決問題。

另一個可替換的技術(shù)是使用C# 4中的dynamic函數(shù),例如,Ruby腳本可以創(chuàng)建類,你可以使用dynamic實例化它們,從C#運行它們的方法,筆者沒有直接嘗試這個方法,在純C#代碼中它可以工作,筆者相信調(diào)用IronRuby代碼時也可以。

小結(jié)

這個新方法不是非常實用,除非你喜歡Ruby或有意深挖它的功能,最大的問題是在托管DLR環(huán)境中缺少調(diào)試選項,你不能單步執(zhí)行Ruby代碼,筆者的調(diào)試方法是,將它們復制到別的IronRuby項目,設置全局變量,復現(xiàn)C#代碼設置,以便筆者單步執(zhí)行代碼,對Ruby高手來說,這是小菜一碟,但對于新手而言,難度可不小。盡管如此,在Windows Phone 7應用程序中使用IronRuby仍然有趣。

如果你想在Windows Phone 7上更多地使用IronRuby,推薦下載iron7,一款適合WP7的 Ruby解釋程序。

【51CTO譯稿,非經(jīng)授權(quán)謝絕轉(zhuǎn)載,合作媒體轉(zhuǎn)載請注明原文出處、作者及51CTO譯稿和譯者!】

原文出處

原文名:Use IronRuby to develop a Windows Phone 7 app

作者:Justin James

小貼士:“IronRuby”--即Ruby語言的一個.NET版本,它基于Dynamic Language Runtime (DLR)構(gòu)建,可以用來開發(fā).net應用程序。

【編輯推薦】

  1. Windows Phone 7 動手實驗室“內(nèi)幕”曝光!
  2. 諾基亞向開發(fā)者贈送E7及Windows Phone 7手機
  3. 微軟發(fā)布Windows Phone 7開發(fā)者向?qū)?/a>
  4. Windows Phone 7發(fā)布第一個更新
  5. 微軟向開源團體發(fā)布“IronRuby”語言
責任編輯:佚名 來源: 51CTO
相關(guān)推薦

2010-12-01 09:01:31

獨立存儲Windows Pho

2010-11-03 15:10:04

SilverlightSilverlightWindows Pho

2011-04-08 10:02:06

日歷Windows Pho

2011-04-01 13:20:40

Windows Pho應用程序

2012-05-17 14:15:10

Windows Pho

2013-07-30 13:38:27

Windows PhoWindows Pho

2013-07-31 14:50:32

Windows PhoWP應用程序生命周期

2011-10-25 10:24:03

Windows Pho

2012-05-28 15:37:20

WP程序生命周期

2013-07-30 11:18:37

Windows PhoWindows Pho

2010-08-27 09:36:57

Windows Pho

2011-06-07 11:35:38

Windows Pho

2012-08-16 10:35:50

Windows Pho

2011-02-22 10:23:43

2010-12-14 18:48:49

微軟

2011-06-08 10:24:38

Windows Pho 應用程序

2011-06-08 10:01:36

Windows Pho 應用程序

2011-12-03 21:03:14

Windows Pho

2010-10-29 14:08:01

.NETWindows PhoiPhone

2010-08-10 11:11:31

點贊
收藏

51CTO技術(shù)棧公眾號