使用IronRuby开发Windows Phone 7应用程序

原创
移动开发
本文我们将为大家讲解如何使用IronRuby开发Windows Phone 7应用程序,由于笔者最近在研究IronRuby,因此也想尝试一下用IronRuby开发Windows Phone 7应用程序,因此有了Lifting Calculator这款程序。

【51CTO译文】微软发布Windows Phone 7平台后,Windows Phone发展并不是非常迅速,但是诺基亚牵手微软后使更多开发者看到了Windows Phone 7平台的潜力,51CTO也歇尽全力为不同语言的开发者打造一个开发Windows Phone应用的平台。前不久51CTO给大家奉献过《写给Android开发者的Windows Phone开发秘籍》,本文我们将为大家讲解一个外国开发者贾斯汀 詹姆斯(Justin James)如何使用IronRuby开发Windows Phone 7应用程序,在未来51CTO将为大家讲述解不同语言开发Windows Phone 7应用的实例。

IronRuby

以下为全部译文

数周前,笔者在微软赞助的一个比赛中免费获得了一部三星Focus手机,激起了笔者开发一个Windows Phone 7应用程序的欲望,由于笔者最近在研究IronRuby,因此也想尝试一下用IronRuby开发Windows Phone 7应用程序,因此有了Lifting Calculator这款程序。

使用IronRuby开发Windows Phone 7应用程序

下载IronRuby

笔者的电脑上安装了IronRuby 1.1.1,但笔者更熟悉IronRuby 1.1.2,并且它包括了Windows Phone 7支持,因此笔者下载了二进制包(MSI版本不友好)。

使用IronRuby编写一个Windows Phone 7应用程序

笔者曾拜读过Shay Friedman发表在MSDN上一篇题为“Windows Phone 7上的IronRuby”的文章,笔者调整了他的方向以满足笔者的需要,一方面是因为笔者已经完成了一部分功能,另一方面,笔者想尝试一下如何使用IronRuby,此外,那篇文章给出的说明不是完全正确。

下面是笔者采取的步骤:

◆添加IronRuby包的引用,笔者从目录中添加了所有Windows Phone 7二进制包。

◆准备XAML,添加需要的代码(笔者还没有使用MVVM)。

◆在调用Ruby的C#代码中,笔者添加了下面的Using语句:

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

◆创建Ruby文件,包含需要的函数,Ruby脚本应该操作通过Silverlight主机传递的全局变量,然后返回一个输出值。

◆笔者将生成操作设置为将新Ruby文件复制到输出目录。

◆添加调用IronRuby脚本的代码,通过一个变量接收它的输出。

详细信息请查看示例代码A(调用IronRuby脚本的C#代码)和B(IronRuby脚本)。

在Shay的文章中,他将整个应用程序传递给了脚本,相反,笔者只传递了表示输入参数的对象和容纳输出的对象,运行这个脚本时,将输出信息显示在屏幕上。

示例代码A:调用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  

 

笔者学到了一个非常重要的教训:外部全局变量设置必须使用大写名称,否则变量将不起作用。更令人沮丧的是,在笔者的脚本中,全局变量名称前有一个美元符号前缀,这样做本身并没有错,变量也会初始化,但却没有输出,笔者在这个上面花了大量时间才解决问题。

另一个可替换的技术是使用C# 4中的dynamic函数,例如,Ruby脚本可以创建类,你可以使用dynamic实例化它们,从C#运行它们的方法,笔者没有直接尝试这个方法,在纯C#代码中它可以工作,笔者相信调用IronRuby代码时也可以。

小结

这个新方法不是非常实用,除非你喜欢Ruby或有意深挖它的功能,最大的问题是在托管DLR环境中缺少调试选项,你不能单步执行Ruby代码,笔者的调试方法是,将它们复制到别的IronRuby项目,设置全局变量,复现C#代码设置,以便笔者单步执行代码,对Ruby高手来说,这是小菜一碟,但对于新手而言,难度可不小。尽管如此,在Windows Phone 7应用程序中使用IronRuby仍然有趣。

如果你想在Windows Phone 7上更多地使用IronRuby,推荐下载iron7,一款适合WP7的 Ruby解释程序。

【51CTO译稿,非经授权谢绝转载,合作媒体转载请注明原文出处、作者及51CTO译稿和译者!】

原文出处

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

作者:Justin James

小贴士:“IronRuby”--即Ruby语言的一个.NET版本,它基于Dynamic Language Runtime (DLR)构建,可以用来开发.net应用程序。

【编辑推荐】

  1. Windows Phone 7 动手实验室“内幕”曝光!
  2. 诺基亚向开发者赠送E7及Windows Phone 7手机
  3. 微软发布Windows Phone 7开发者向导
  4. Windows Phone 7发布第一个更新
  5. 微软向开源团体发布“IronRuby”语言
责任编辑:佚名 来源: 51CTO
相关推荐

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

2011-06-08 10:24:38

Windows Pho 应用程序

2011-12-03 21:03:14

Windows Pho

2011-06-08 10:01:36

Windows Pho 应用程序

2010-12-14 18:48:49

微软

2010-10-29 14:08:01

.NETWindows PhoiPhone

2010-08-10 11:11:31

点赞
收藏

51CTO技术栈公众号