IronRuby中的WPF示例通过C#与Wesabe对话

wesabeironrubymoney

John Lam and team have released their first drop the IronRubySource code. It's super-pre-alpha so don't hate, but it's looking pretty sweet, if you're a burgeoning Ruby-head like myself. There's incomplete support for some of the cooler Ruby syntax things, but I have full confidence that it's only going to get better. All the underpinnings are there.

John Lam和团队已经发布了他们的第一批IronRubySource代码。 它是超级pre-alpha版本,所以不要讨厌,但如果您像我这样蓬勃发展的Ruby头,它看起来就很可爱。 对一些凉爽的Ruby语法的支持还不完全,但是我完全有信心,它只会变得更好。 所有的基础都在那里。

John also announce that the team will host IronRuby source on Rubyforge (which is quite the coup, actually) and that they'll be taking external contributions very soon (this being the open part of open source.) 

John还宣布,该团队将在Rubyforge上托管IronRuby的源代码(实际上这是一个政变),并且他们将很快做出外部贡献(这是开源的开放部分)。

Not every sample (initially) will look and feel and smell good like Ruby, and this one is no exception. You might remember that last week I did a quicky Wesabe client in C# and put it up on Google Code. We've got four folks up there improving the code, which is cool.

并非每个样本(最初)都会像Ruby一样具有良好的外观和感觉以及气味,而这个样本也不例外。 您可能还记得,上周我在C#中做了一个快速的Wesabe客户程序并将其放在Google Code上。 我们那里有四个人在改进代码,这很酷。

Today, Shady the intern and I decided to do a sample in IronRuby that would call the C# Wesabe client API and display some account data via WPF. This is part of my new plan to take over Money and Quicken's business and you can tell from the hour s of intense UI design that Shady and I did, that my plan is inevitable.

今天,实习生Shady和我决定在IronRuby中做一个示例,该示例将调用C# Wesabe客户端API并通过WPF显示一些帐户数据。 这是我新的计划来接管Money和Quicken的业务的一部分,你可以告诉从小时 S 的强烈的UI设计,阴凉,我做到了,我的计划是不可避免的。

We started from ScottGu's HelloWorld sample, and slogged creatively coded our way forward. Here's how it went.

我们开始ScottGu的HelloWorld示例,并 艰难地 创造性地编码我们前进的方向。 这是怎么回事。

image

First, get the IronRuby source and upzip. Open on Visual Studio 2005 command prompt and compile the DLR and IronRuby by running build.cmd.

首先,获得IronRuby的源代码和upzip。 在Visual Studio 2005命令提示符上打开,然后通过运行build.cmd编译DLR和IronRuby。

All the interesting stuff will show up in bin/release, including the IronRuby equivalent of IRB, the interactive Ruby Interpreter.  You can run this from the command line and try things out interactively if you like.

所有有趣的内容都会显示在bin / release中,包括与IRB等效的IronRuby(交互式Ruby解释器)。 您可以从命令行运行它,并且可以根据需要以交互方式进行尝试。

We used Notepad2 with Ruby Support for our editor, although I'm deeply digging "e" the TextMate for Windows and will likely move over to it for my text editor of choice when I start at Microsoft in September.

我们使用Notepad2和Ruby Support作为编辑器,尽管我正在深入研究Windows版TextMate的“ e”功能,并且当我9月在Microsoft入职时,很可能会选择我的文本编辑器使用它。

Next, we tried to add a require statement to bring in the Wesabe library. You run a IronRuby app (today, in this build) by running rbx.exe YourApp.rb. We put our .rb text file in one folder and had rbx.exe in another, so when we added our wesabelib.dll we had to add it to the same folder that rbx.exe was in, otherwise the Assembly load would fail. You can also put things in the GAC and get to them there. Just remember that Fusion will load from the same folder rbx.exe is in, not the folder that your program is in.

接下来,我们尝试添加一个require语句以引入Wesabe库。 您通过运行rbx.exe YourApp.rb运行Ironiron应用程序(今天,在此版本中) 我们将.rb文本文件放在一个文件夹中,而将rbx.exe放在另一个文件夹中,因此,当我们添加wesabelib.dll时,必须将其添加到rbx.exe所在的同一文件夹中,否则,程序集加载将失败。 您也可以将内容放入GAC中并到达那里。 请记住,Fusion将从rbx.exe所在的文件夹而不是程序所在的文件夹加载。

For debugging, we used the classic "got here" debugging of old, via the standard MessageBox, so we added a requre for Windows Forms also and a constant as a alias for the MessageBox class:

为了进行调试,我们通过标准的MessageBox使用了经典的“ get here”调试功能,因此,我们还为Windows Forms添加了要求,并为MessageBox类添加了一个常量作为别名:

require 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
require 'wesabelib, Version=1.0.0.0'
MessageBox = System::Windows::Forms::MessageBox

As this build is a pre-Alpha, likely created in the dark of the night just hours ago, there's all sorts of weirdness and stuff that didn't "just work."

由于此版本是Alpha之前的版本,可能是在几个小时前的黑夜中创建的,因此存在各种奇怪的内容和“不起作用”的内容。

The naming integration with .NET is persnickety at best. The C# Wesabe client had a lowercase class called "wesaberest" that IronRuby just couldn't see, so I changed it to uppercase and aliased it with a constant.

与.NET的命名集成充其量是顽强的。 C#Wesabe客户端具有IronRuby看不到的小写类“ wesaberest”,因此我将其更改为大写并使用常量作为别名。

#this build of IronRuby doesn't like Class Names that are lowercase,
# so I made mine Uppercase
Wesabe = Wesaberest

As I said, The Wesabe client API that we created is written in C#, and returns a array of Account objects. In the click event of a button, we pass in the username and password from our TextBox and PasswordBox respectively and store our Account object.

正如我所说,我们创建的Wesabe客户端API是用C#编写的,并返回一个Account对象数组。 在按钮的单击事件中,我们分别从TextBox和PasswordBox中传递用户名和密码,并存储Account对象。

Our goal is to spin through the list of Account objects in the Array, but there's a few gotchas, again because of the pre-alphaness. First, we can't use for because in Ruby for is a sugar syntax on top of "each" that assumes an each method exists. Each isn't implemented yet, but the plan is to have for syntax just work over IEnumerable things.

我们的目标是遍历Array中的Account对象列表,但是又有一些陷阱,同样是由于前字母。 首先,我们不能使用for,因为在Ruby中,for是在“每个”之上的一种糖语法,其中假设每个方法都存在。 每个都尚未实现,但计划是使语法仅在IEnumerable事物上起作用。

Second, we can't index into arrays because the [] operator isn't implemented yet, but we can call GetValue(), which is the same thing.

其次,由于尚未实现[]运算符,因此无法索引数组,但可以调用GetValue(),这是同一件事。

#in the future we'll be able to index into CLR arrays...
#oneAccount = myAccounts[i]
oneAccount = myAccounts.GetValue(i)

Thirdly, in IronRuby there's FixNum, which is a DLR extension of Integer and there's MutableStrings, that have private StringBuilder. A lot of this will be hidden in the future, but in this build I had to be aware of some of the impedance mismatches as I tried to coerce my numbers into strings for output.

第三,在IronRuby中有FixNum,它是Integer的DLR扩展,还有MutableStrings,它们具有私有的StringBuilder。 将来很多东西会被隐藏起来,但是在这个版本中,我不得不注意一些阻抗不匹配,因为我试图将我的数字强制转换成字符串以进行输出。

For example, none of these worked, but I expect they will one day.

例如,这些都不起作用,但是我希望它们有一天会起作用。

Lastly, currentBalance is a float (System.Single) and IronRuby wouldn't respect it's ToString method, so I changed the value to a System.Double and all was fine.

最后,currentBalance是一个浮点数(System.Single),IronRuby不尊重它的ToString方法,因此我将值更改为System.Double,一切都很好。

Here's some failed attempts at String Concatenation. Notice the "secret" call to to_clr_string. Those should all go away and the Ruby types should marshal cleanly in most cases. Note that I'm assuming a lot, and in some cases basing these assumptions on my chats with John, but also as educated guesses as to how it ought to work.

这是字符串连接失败的尝试。 注意to_clr_string的“秘密”调用。 这些应该全部消失,并且在大多数情况下,Ruby类型应该干净利落地封送。 请注意,我做了很多假设,在某些情况下,这些假设是基于与John的聊天,但也有关于它应该如何工作的有根据的猜测。

#Ruby string concatenation works fine...
my_message.content = "scott" + " hanselman"

#Attempt0
my_message.content = oneAccount.name + oneAccount.currentBalance.to_string("C").to_clr_string

#Attempt1
my_message.content = oneAccount.name + "$" + oneAccount.currentBalance.to_s.to_clr_string

#Attempt2
my_message.content = System::String.Concat("$" + oneAccount.currentBalance.to_s.to_clr_string)

#Attempt3
#There's a name conflict when trying to use the StringBuilder:
# System.MemberAccessException: uninitialized constant ScriptModule::Text
s = System::Text::StringBuilder.new
s.Append(oneAccount.name)
s.Append("$")
s.Append(oneAccount.currentBalance.to_s.to_clr_string)
my_message.content = s.to_string

At this point, I just added a new property called "DisplayString" in the Wesabe Account class and moved on. As for the loop we used a while. It's not clean, but it's clean enough:

此时,我刚刚在Wesabe Account类中添加了一个名为“ DisplayString”的新属性,然后继续。 至于循环,我们使用了一段时间。 它不干净,但足够干净:

my_button.click do |sender, args|
   a = Wesabe.getAccounts(text_user.text,text_password.password)
   myAccounts = a.Items
   i = 0
   accounts_listbox.items.clear
   while (i < a.Items.Length)
      oneAccount = myAccounts.GetValue(i) #GetValue is on System.Array

      i+=1 #needed for the while
      
      # Create new label
      my_message = Label.new
      my_message.font_size = 36
      my_message.content = oneAccount.DisplayString

      #Add the Label to the ListBox
      accounts_listbox.items.add(my_message)
   end
end 

There's a couple of ways to make this even more clean, and those ways will come. I'll update this sample as the new features roll into IronRuby.

有两种方法可以使这种情况更加清晰,这些方法将会出现。 随着新功能集成到IronRuby中,我将更新此示例。

I think it'd be nice to add some charts and graphs, make the whole thing a ClickOnce app, etc. Probably, for now, it'll be easier in C#, but it was fun to hack it together in today's drop of IronRuby and the interns learned something.

我认为最好添加一些图表和图形,使整个事情都成为ClickOnce应用程序,等等。也许到目前为止,在C#中会更容易,但是在今天的IronRuby中将它们结合在一起很有趣。实习生学到了一些东西。

You can get my source for this Ruby App, along with a custom build of the Wesabe client (with my changes) here. Remember to get the IronRuby source, compile it, then copy the Wesabe client to the bin\release folder (or move the .rb files into there along with the Wesabe dll). I've left all the comments in the source for fun.

您可以在此处获得此Ruby应用程序的源代码以及Wesabe客户端的自定义版本(包含我的更改)。 请记住要获取IronRuby源代码,对其进行编译,然后将Wesabe客户端复制到bin \ release文件夹(或将.rb文件和Wesabe dll一起移动到该文件夹​​中)。 我把所有评论都留在了源中以示乐趣。

翻译自: https://www.hanselman.com/blog/wpf-sample-in-ironruby-talking-via-c-to-wesabe

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值