It’s already 6 years since my first post about MVVM and data bindings in WPF MVVM – ListView SelectedItem and DataBinding. The MVVM pattern is now quite popular and comes into use in many WPF applications. However a lot of things have changed since 2008. We have now many new platforms where we can also apply MVVM pattern such as Windows 8 app, Windows phone app… When you use Xamarin (http://xamarin.com/) to develop cross platform applications with C#, maybe you want to use MVVM pattern too. In my last post 6 years ago, I introduced to you the concept of MVVM in ‘raw’ format which means no use of any external frameworks. Today I would like to make a supplementary post by introducing 2 MVVM frameworks for easing the use of MVVM pattern. The first one is Caliburn.Micro https://github.com/Caliburn-Micro/Caliburn.Micro and the second one is Mvvm Cross https://github.com/MvvmCross/MvvmCross . They’re both open source products and can be used in any commercial applications.
If you’re working with .NET, maybe you’re already familiar with the concepts of dependency injection and MVVM (Model-View-ViewModel) pattern. Some of popular dependency injection containers for .NET are Castle Windsor, Ninject or Unity… They are all good frameworks, it just depends on which you prefer. For example, I use Castle Windsor at company and Ninject at home so that I can understand and use both of frameworks. It’s just the matter of favorite. The MVVM pattern was introduced years ago when WPF came to market. This pattern makes use of data binding structure of WPF and helps programmer to separate the view completely from logic layer. For more details about this pattern you can read some articles on my site about MVVM. Today, I will discuss again about these concepts, however not for .NET application, but I would like to show how we can apply these concepts into Android app. The example I use in this post is a De-De-Dictionary app. In this app, I can add German vocabulary with his explanation (also in German :)) through a user interface or look up for a vocabulary. Nothing special than a real dictionary book.
MVVM pattern is the most popular pattern to developers in WPF environment and there are already many platforms/frameworks supporting this pattern out there in internet. For example, I use Caliburn Micro Framework at work and use MVVM Light Toolkit at home for my private studying. Each framework has its own advantages and disadvantages so just choose one which you like or it satisfies you at most. Today I would like to make a small post to describe how the Messenger of MVVM Light Toolkit (MLT) works so that we can make the communication within application without violating the MVVM pattern.
MVVM – Model View ViewModel is the new design pattern originated from Microsoft, introduced by Martin Flower in 2004 and widely used after a publication of Josh Smith on MSDN magazine in 2009. MVVM is targeted at modern user interface development platforms such as WPF and Silverlight which are completely differently designed to traditional developer. Although MVVM bases on MVC – Model View Controller, the ViewModel is more better because it provides an easier way to expose and cosume data objects from the Model and therefore can handle the display logic of View. Despite of the great data binding functions of WPF, in MVVM we do not have to write any code in the View layer anymore. The View and Model layer are completely seperated which means the designers can use native WPF markup language XAML and create bindings to the ViewModel ,which is written by application developers, without writing any code behind the View. The designers can then focus on their designs rather than developing the business logic behind and therefore the software development can be better controlled,easier for bugfixing and transparenter. That’s short description about MVVM and its advantages. I think MVVM is not the hot topic anymore so if you want, you can find a lot of interesting articles on internet. In this blog post, I just want to try the MVVM Light Toolkit to generate automatically a MVVM application for me when I create a new application because until now I must create the pattern myself manually.
UPDATE: I recommend a new version of this post at following link C# – WPF MVVM DataBinding 2014
On studying how to use MVVM pattern in WPF I found out that the ListView or ListBox control doesn’t support ICommand interface and therefore I can not bind a command to a ListView. After searching on Internet I saw that man can use Prism extensions framework http://www.codeplex.com/CompositeWPF to add custom command to our ListView control. The example below guides step by step how to get SelectedItem of ListView in MVVM pattern using command.