问题
I've been very happily using the Model-View-ViewModel (MVVM) pattern in WPF and Silverlight apps in combination with C#. Declarative XAML markup and data binding are invaluable - I just can't live without them. But, this talk by Harry Pierson on dynamic languages got me excited about learning a dynamic language, and I'd like to try one out in a new project. I've been enjoying reading IronPython In Action, and it does contain a few WPF examples - but only with imperative-style code.
What are your thoughts on using IronPython or IronRuby at the ViewModel and Model layers in MVVM apps (compared to C#)? Which features of make them attractive (or unattractive)? I'm interested in fundamental advantages/limitations (e.g. AOP, duck typing, monkey patching, static type limitations, etc) and practical ones alike (e.g. performance, no current IronPython Studio for v2, etc). Will any limitations improve with C# 4.0?
Thanks,
David
回答1:
Shay is right, but only for Silverlight and I've not used Silverlight and IronRuby. You can bind and use commands with pure IronRuby and WPF. I'm assuming this also applies to IronPython as they both use ICustomTypeDescriptor. there is a caveat, you'll need a DLR build more recent than IronRuby 0.9 for events.
To create an IronRuby command, you need to implement events. See this SO question for more Implementing and Interface in IronRuby that includes CLR Events
As for creating an IronRuby object that can participate in binding, the deal is a an attr reader/writer needs to be used for the DLR to see it as a property.
class Bindable
attr :some_property, true
end
I've got a few Gists about it. A Command Example, A simple ViewModel Example with Binding And the XML Builder based xaml library used in the examples. These worked for me.
There are limitations. XamlLoader can't see IronRuby classes from the CLR. This means your views are REALLY dumb (zero code-behind), or they come from a C# dll. It also means that you can't create custom or user controls that have any code behind without going to C#.
While I don't have a fully baked MVVM app in IronRuby/WPF, I think all the pieces are in place and I'm getting there.
回答2:
I can say that writing WPF with IronRuby is pretty cool, and if you invest a little time metaprogramming your environment, it can be awesome.
However, there are the downsides - data binding doesn't work with IronRuby objects (you must use CLR objects for that - look at my blog post for the workaround), but this should improve on .Net 4.
Pay attention that IronRuby is expected to go RTM only in November.
Having said all that, I do recommend you check it out. You might fall for it like I have :)
Shay.
回答3:
I've blogged about my experiences trying out IronPython and MVVM with WPF here. It was surprisingly easy to get going.
来源:https://stackoverflow.com/questions/1458332/are-ironpython-or-ironruby-well-suited-for-the-mvvm-pattern-in-wpf-silverlight