-
Prism 4.0 First Drop – MVVM
5 CommentsThe p&p Team has just released the first drop of Prism 4.0 (previously known as the Composite Application Guidance for WPF and Silverlight). You can download this new version from the codeplex site.
What’s new
In this first drop, you will find 2 new QuickStarts that tackles two of the main concerns of the community: MEF integration and the MVVM pattern. In this post I’ll cover the MVVM QuickStarts and leave the MEF QuickStarts for the following post.
Basic Model-View-ViewModel Quickstart
The BasicMVVM QuickStarts address the most usual challenges when developing MVVM triads. The QuickStarts demonstrate a POCO scenario, showing a questionnaire with different question types that must be completed. By design, the QuickStarts targets Silverlight only (no WPF version) and does not use any of the Prism library .
(Styling is WIP
)Some of the challenges demonstrated in this quickstart are:
- INotifyPropertyChange implementation
Every ViewModel needs to notify the view when its bounded properties change. For that purpose, Silverlight provides the INotifyPropertyChange interface. The QuickStarts provides the ViewModelBase class, a base implementation that provides useful methods for property notification:
protected virtual void RaisePropertyChanged(string propertyName);
protected void RaisePropertyChanged(params string[] propertyNames);
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion);Besides the classic RaisePropertyChaged method that received the name of the property that has changed, it also provides an interesting overload that accepts a lambda expression, which allows to write property notification in the ViewModel as follows:
- Validation
- Transitions/Animations triggered by the ViewModel
this.RaisePropertyChanged(() => this.Name);
Another typical challenge is notifying the View when binding errors occur. For that reason, Silverlight provides two interfaces: INotifyDataErrorInfo and IDataErrorInfo. The ViewModelBase class provides a base implementation for the INotifyDataErrorInfo that can be leveraged from the ViewModels:
public virtual bool HasErrors;
public virtual IEnumerable GetErrors(string propertyName);
protected void SetError(string propertyName, string error);
protected void SetError<T>(Expression<Func<T>> propertyExpresssion, string error);
protected void ClearErrors(string propertyName);
protected void ClearErrors<T>(Expression<Func<T>> propertyExpresssion);
protected void SetErrors(string propertyName, List<string> propertyErrors);
protected void SetErrors<T>(Expression<Func<T>> propertyExpresssion, List<string> propertyErrors);
protected virtual void RaiseErrorsChanged(string propertyName);
protected virtual void RaiseErrorsChanged<T>(Expression<Func<T>> propertyExpresssion);The quickstart demonstrate how the domain model validation is done on the Model (for example the NumericQuestions). Formatting validation is performed on the ViewModels (for example the Age property in the questionnaire) to customize error messages (if you don’t need customization of error messages you can just rely on Silverlight binding errors).
Animations are typically triggered by the UI (e.g. when clicking a button). But some times animations must start based on a change in the ViewModel. The challenge in those cases is how to start the animation when the ViewModel doesn’t have a reference to the View. The quickstart demonstrate how to use Triggers and the VisualStateManager bound to a property in the ViewModel to accomplish that requirement:
- Usage of hierarchical ViewModels (ViewModels that contain other ViewModels)
<i:Interaction.Triggers>
<ei:DataTrigger Binding=“{Binding CurrentState}“ Value=“Normal“>
<ei:GoToStateAction StateName=“Normal“/>
</ei:DataTrigger>
<ei:DataTrigger Binding=“{Binding CurrentState}“ Value=“Submitting“>
<ei:GoToStateAction StateName=“Submitting“/>
</ei:DataTrigger>
</i:Interaction.Triggers>Each of the questions in the questionnaire has their own ViewModel, and the QuestionnaireViewModel has a property of type List<QuestionViewModel> Questions. In the QuestionnaireView, the questions’ view models are bound to the corresponding view DataTemplate. This is achieved by the newly developed DataTemplateSelector (WPF provides a mechanisms for doing this out-of-the-box, Silverlight does not.)
<ItemsControl.ItemTemplate>
<DataTemplate>
<Infra:DataTemplateSelector Content=“{Binding}“ HorizontalContentAlignment=“Stretch“ IsTabStop=“False“>
<Infra:DataTemplateSelector.Resources>
<DataTemplate x:Key=“OpenQuestionViewModel“>
<!– View for OpenQuestionViewModel –>
</DataTemplate><DataTemplate x:Key=“MultipleSelectionQuestionViewModel“>
<!– View for MultipleSelectionQuestionViewModel –>
</DataTemplate>
<DataTemplate x:Key=“NumericQuestionViewModel“>
<!– View for NumericQuestionViewModel –>
</DataTemplate>
</Infra:DataTemplateSelector.Resources>
</Infra:DataTemplateSelector>
</DataTemplate>
</ItemsControl.ItemTemplate>- Use of Blend Behaviors (Commanding, Data Template Selection, Binding, Validation)
Finally, the quickstart demonstrate the usage of several new Behaviors to simplify the binding between the View and the ViewModel. Some of these behaviors are:
- CallExecuteMethod: This behavior is similar to binding to a command, but removes the need to explicitly instantiate a Command in the ViewModel. You can specify the method to execute and a CanExecute property:
<Behaviors:CallExecuteMethodAction TargetObject=”{Binding}” ExecuteMethodName=”Submit” CanExecutePropertyName=”CanSubmit”/>
- UpdateTextBindingOnPropertyChanged: This behavior updates the binding every time the text property changes on a TextBox. This can be done in WPF without out-of-the-box behaviors , but is not available in Silverlight yet.
- ValidateObject: This behavior updates the ValidationSummary property for the ValidationSummary controls whenever an error occurs on its source.
<TextBox Text=”{Binding ResponseText, Mode=TwoWay, UpdateSourceTrigger=Explicit}”>
<i:Interaction.Behaviors>
<Behaviors:UpdateTextBindingOnPropertyChanged/>
</i:Interaction.Behaviors>
</TextBox><sdk:ValidationSummary >
<i:Interaction.Behaviors>
<Behaviors:ValidateObject SourceObject=”{Binding}”/>
</i:Interaction.Behaviors>
</sdk:ValidationSummary>All these leverage Blend’s Behavior<T> class, so they can also be used with drag&drop from Blend.
This is the first drop of the new version of prism, so any feedback will be really appreciated from the p&p team and will help improving future drops. Remember to leave your feedback in the codeplex site.
-
5 Comments:
Leave a comment
Your email address will not be published.
Twitter Trackbacks for Blogged: Prism 4.0 First Drop – MVVM [southworks.net] on Topsy.com said on June 3, 2010:
[...] Blogged: Prism 4.0 First Drop – MVVM blogs.southworks.net/matiasb/2010/06/03/prism-40-first-drop-mvvm/ – view page – cached Tweets about this link Topsy.Data.Twitter.User['clientdev'] = {“photo”:”http://a1.twimg.com/profile_images/358901466/twit_normal.png”,”url”:”http://twitter.com/clientdev”,”nick”:”clientdev”}; clientdev: “RT @southworks: Blogged: Prism 4.0 First Drop – MVVM http://goo.gl/fb/5Bdv0 ” 18 minutes ago retweet Topsy.Data.Twitter.User['silverfighter'] = {“photo”:”http://a3.twimg.com/profile_images/321112405/twitterProfilePhoto_normal.jpg”,”url”:”http://twitter.com/silverfighter”,”nick”:”silverfighter”}; silverfighter: “RT @southworks: Blogged: Prism 4.0 First Drop – #MVVM http://goo.gl/fb/5Bdv0 ” 33 minutes ago retweet Topsy.Data.Twitter.User['southworks'] = {“photo”:”http://a3.twimg.com/profile_images/727738241/SWsmall_normal.png”,”url”:”http://twitter.com/southworks”,”nick”:”southworks”}; southworks: “Blogged: Prism 4.0 First Drop – MVVM http://goo.gl/fb/5Bdv0 ” 33 minutes ago retweet Filter tweets [...]
DotNetShoutout said on June 4, 2010:
Prism 4.0 First Drop – MVVM…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
mvvm » Twitter Trends said on June 6, 2010:
…
RT @SilverlightNews: Prism 4.0 First Drop – MVVM – full link – …
Prism v4 is Rolling Out said on June 8, 2010:
[...] Prism v4 first drop. [...]
Diego Poza’s Blog » Blog Archive » Prism 4: What’s new and what’s next said on June 11, 2010:
[...] For more information about the MVVM QuickStart, see Matias Bonaventura’s post. [...]