Wednesday, December 29, 2010

 

Reusing the ViewModel in MVVM for WP7

Edit: There turned out to be a way to do this without having to deal with the warning message that appears from WP7 using a Silverlight Class Library. The altertive way retains the write once ViewModel, but compiles the ViewModel seperately in each project it's used in: http://feddas.blogspot.com/2011/03/sharing-viewmodel-in-mvvm.html

I am creating a game for the WP7 that I decided I wanted to be a pay to play game. One important step that became evident is the need for a trail version. I wanted the trail version to stay up to date with minimal effort as I update the rest of the game. To do this I decided to make use of the MVVM model, using a single ViewModel for both the View of the paid version and the View of the trail version.

I thought this seperation would be a daunting task as I had little experience creating multi-project solutions. However, this is surprisingly simple. The extreme simplicity of it explains why I was unable to find much information while searching the internet. Following are the steps I took.

  1. Install MVVM Light V3 Binaries and Templates for your flavor of Visual Studio
    http://www.galasoft.ch/mvvm/installing/manually/

  2. Install the WP7 RTM hotfix
    http://blog.galasoft.ch/archive/2010/07/22/mvvm-light-hotfix-for-windows-phone-7-developer-tools-beta.aspx

  3. If you did not unblock the MVVM binaries .Zip file before you installed you can either unblock that .Zip file now and reinstall (the recommend method) or individually unblock all of the installed MVVM Light binaries for WP7 and Silverlight 3.

    Location of the to Unblock button in a files properties


  4. Create a new MvvmLight (WP7) project with any name, I named if after a game I want to create called BouncyLasersMvvm.

    Creating a new MvvmLight (WP7) solution


  5. Add a Silverlight Class Library named “ViewModel” that is targeted to Silverlight Version 3. I choose Silverlight 3 because it is the CLR that WP7 uses.

    Add Silverlight Class Library project


  6. Click and drag the ViewModel Folder into the ViewModel Project to create a copy.

    Copy the ViewModel folder from the View project into the ViewModel project

  7. In the ViewModel Project Add a reference to all 3 GalaSoft MVVM libraries; "System.Windows.Interactivity.dll" "GalaSoft.MvvmLight.dll" and "GalaSoft.MvvmLight.Extras.dll"

    Add all of the MVVM Light libraries

  8. Delete the Model and ViewModel folders from the MVVM project and add a reference to the ViewModel project.

    Delete ViewModel data from the View Project


  9. In the App.xaml of the View project, change the value of xmlns:vm to
    [your solution name].ViewModel;assembly=ViewModel
    xmlns:vm="clr-namespace:BouncyLasersMvvm.ViewModel;assembly=ViewModel"

  10. In the codebehind for the App.xaml, App.xaml.cs, change the last using statement, that should now have a red line beneath it, to use the same namespace you referenced in the App.xaml in step 9. In this example it would be using BouncyLasersMvvm.ViewModel

That’s it. If you’d like to create more Views add new MVVM Light projects. Then repeat steps 8 and 10. You can now make Views for trail versions or Views for demos to put on a website using silverlight.

When you modify the text used for the Welcome property inside of ViewModel/MainViewModel.cs and swap the project that is run using “Set as startup project” you will see that all the Views you create now use the same ViewModel.

Edit: Limitations of a Silverlight 3 ViewModel for WP7 (from http://social.msdn.microsoft.com/Forums/en-US/windowsphone7series/thread/79858a29-4db0-460d-9a75-3630211a28fb/):
"You can reuse a Silverlight 3 Class Library between a web and WP7 project without any problems as long as you follow the following guidelines:

It's not as limiting as it sounds, in fact the entire Silverlight Toolkit is binary compatible between the web and WP7"

Labels: , ,


Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]