Wednesday, March 09, 2011

Using MvvmLight with (XAML) Silverligtht 4 or WPF

Introduction

This tutorial will try and explain the advantages and the 'how to' of using MVVMLight within your XAML application. So that you can produce code that easier mantainable and more importantly testable.

Why Use The MVVM Pattern

The tight coupling of GUI to code behind has over the years meant that testing the code-behind can be some what tricky. In that how do you mimic the selected event in the GUI to call the binded event code - you can mimic teh call to your event code but how do you get the selected item from the combo (in the GUI) that doesn't exist, as you are not running the GUI but testing the methods that the GUI uses.
What was needed was a loose coupling between the GUI and the code behind, this is where Context classes come in - XAML (Silverlight or WPF) have a concept where a Silverlight page can have a (context) class associated with it for it's events and also (more importantly) have setters and getters associated with the bindings to gui controls. So that, when a property is updated by it's setter - a call to your 'NotifyPropertyChanged' method in the class 'INotifyPropertyChanged' will trigger the rebinding to the control.

Install And Integrating MVVMLight Into Your Project
You can download and follow the instructions from Gala's web site here http://www.galasoft.ch/mvvm/getstarted/

OR

Get NUGet (which is a handy plugin for Visual Studio - http://nuget.codeplex.com/releases) to convert your application into a MVVMLight application.
How to add MVVMLight dlls to your project.
Use this tutorial as a guide to adding the dll's to your project
Below is how to convert you project to an MVVMLight project after installing NUGet and running the MVVMLight installer.

Read more: Codeproject