Apart from all the goodness the new API’s in “Mango” bring us, there is that other great gift that it brings us: Silverlight 4. After spending so much time on Windows Phone 7 you would almost forget that there’s life after the ‘Silverlight 3+’ version that was the baseline up till now. This means for MVVMLight fans like me:
- You can now directly bind a command to a button’s “Command” property. In fact, you can bind a command to anything that derives from ButtonBase. This includes HyperlinkButton, RadioButton, and CheckBox (and possibly more). If you bind commands to these user interface elements, you can now say goodbye to your good old friends EventTrigger and EventToCommand
- If you want to use data from your model in a behavior, you can now bind directly bind data to dependency properties defined in the behavior itself, in stead of doing the attached dependency property dance, or resorting to the MVVMLight Messenger.
The first one saves a bucket load of XAML. Before Mango, you had to create something like this to fire a command by a button being clicked:
<Button Content="Start game"> <Interactivity:Interaction.Triggers> <Interactivity:EventTrigger EventName="Click"> <MvvmLight_Command:EventToCommand Command="{Binding StartNewGame, Mode=OneWay}"/> </Interactivity:EventTrigger> </Interactivity:Interaction.Triggers> </Button>
Now you can write the same functionality like this:
<Button Content="Start game" Command="{Binding StartNewGame, Mode=OneWay}"/>
By no means this means the end of EventToCommand. First of all, it needs to be there for backward compatibility (unless you particularly like rewriting working XAML) .Second, there are plenty of cases for user interface elements that generate particular events that you might want to route to a model via a command. For example, I routinely signal my model a page has completed loading by attaching a command to it’s “Loaded” event. That still requires the EventTrigger-EventToCommand route.
For behaviors, the gain is a little bit less visible. But remember my little Windows Phone Drag/Flick behavior? That sports a dependency property BrakeSpeed that I could now bind to my ViewModel on all instances, thereby controlling all behaviors’ brake speed with one property. The same goes for my SetInitialOpacityBehavior, where I explicitly state that you cannot bind to its OpacityAfterLoading property: “in Windows Phone 7 you cannot data bind it because it’s currently based on Silverlight 3 which only allow you to bind to classes that do descend from DependencyObject – which a behavior obviously does not ;-)”. Well, now you can, and this gives you a powerful and easy way to control behaviors from your ViewModel, and let behaviors push data back to it.
All in all, the ‘Silverlight4+’ in Windows Phone 7 “Mango” gives us all the opportunities to make MVVMLight based applications even more easy and clean than they already were.
3 comments:
How about enabling and disabling with relaycommands? I can't get this working.
How about the IsEnabled property? Can't get this working.
Joey, a way to make this work is to make another property IsEnabled and binding that to the IsEnabled property of the button. Is that what you mean?
Post a Comment