Slides and Code from my Silverlight MVVM Talk at DevConnections

image

I had a great time at the DevConnections conference in Las Vegas this year where Visual Studio 2010 and Silverlight 4 were launched. While at the conference I had the opportunity to give a full-day Silverlight workshop as well as 4 different talks and met a lot of people developing applications in Silverlight. I also had a chance to appear on a live broadcast of Channel 9 with John Papa, Ward Bell and Shawn Wildermuth, record a video with Rick Strahl covering jQuery versus Silverlight and record a few podcasts on Silverlight and ASP.NET MVC 2.  It was a really busy 4 days but I had a lot of fun chatting with people and hearing about different business problems they were solving with ASP.NET and/or Silverlight. Thanks to everyone who attended my sessions and took the time to ask questions and stop by to talk one-on-one.

One of the talks I gave covered the Model-View-ViewModel pattern and how it can be used to build architecturally sound applications. Topics covered in the talk included:

  • Understanding the MVVM pattern
  • Benefits of the MVVM pattern
  • Creating a ViewModel class
  • Integrating a Service Agent
  • Loading a Service Agent using MEF (shown in the sample)
  • Implementing INotifyPropertyChanged in a ViewModelBase class
  • Binding a ViewModel declaratively in XAML
  • Binding a ViewModel with code
  • ICommand and ButtonBase commanding support in Silverlight 4
  • Using InvokeCommandBehavior to handle additional commanding needs
  • Working with ViewModels and Sample Data in Blend
  • Messaging support with EventBus classes, EventAggregator and Messenger
  • My personal take on code in a code-beside file (I’m all in favor of it when used appropriately for message boxes, child windows, animations, etc.)

One of the samples I showed in the talk was intended to teach all of the concepts mentioned above while keeping things as simple as possible.  The sample demonstrates quite a few things you can do with Silverlight and the MVVM pattern so check it out and feel free to leave feedback about things you like, things you’d do differently or anything else. MVVM is simply a pattern, not a way of life so there are many different ways to implement it.

If you’re new to the subject of MVVM check out the following resources. I wish this talk would’ve been recorded (especially since my live and canned demos all worked :-)) but these resources will help get you going quickly.

 

image_418B5BFB[1]

 

Download sample code and slides from my DevConnections talk

 

 

Logo

For more information about onsite, online and video training, mentoring and consulting solutions for .NET, SharePoint or Silverlight please visit http://www.thewahlingroup.com.

comments powered by Disqus

12 Comments

  • Hi Dan,

    Great post!

    No matter how hard I try, I cannot get your sample code to run as the XAML will not recognise the interactivity/interactions namespace declarations (yes, I do have Blend 3 installed). Do you happen to know why this might be?

    Cheers,

    sfx

  • Hi Dan,
    I was at the conference and I have to say that your Silverlight sessions were the most enjoyable and useful of all the sessions I attended. Being a complete Silverlight noob, I really enjoyed your other talks on getting started with Silverlight as well...is any of the sample code/slides from those sessions available online?

    Thanks,
    Matt

  • Looks great, thanks for the PowerPOint and the code example

    I guess the only thing I am doing differently in my "Simple MVVM" is that I am not using Methods. All events are an ICommand that allow me to pass in a parameter. In most cases I am passing in UI elements to the ViewModel and telling it to hold a reference and even attach events if needed. I think this is why I have yet to need messaging. My tutorial is here:

    http://www.codeproject.com/KB/silverlight/AdvancedMVVMVideo.aspx

  • Dan,
    Thanks for the code and slides. You were by far the best presenter.

  • sfx,

    Make sure you have Blend 4 installed (latest version is the RC). You can find the correct .dll (if it's not showing up for you in the project) at C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\Silverlight\v4.0\Libraries by default (remove x86 if you're on 32-bit system of course).

    Dan

  • Matt,

    Thanks for the comments. I'm really glad to hear that you found the sessions useful. See you next time. :-)

    Dan

  • Michael,

    I prefer to use properties to hold the data since going that route allows my methods to stay clean and not require parameters in most cases. By passing a UIElement the ViewModel has to know a little about the View which I prefer to steer clear of. I try really hard to make the ViewModel completely ignorant of the actually technology being used to render the views when possible. But...there are many ways to do it of course. :-)

    Dan

  • Lamar,

    Thanks for the comments! Really appreciate it.

    Dan

  • Great session at DevConnections! I attended the "Get Started Building Silverlight Applications". I also wonder if the slides and/or source code was available for download anyhwhere?

    Thanks again for a great session-

  • Hi Dan,

    Thanks for the help. I did not realise that Blend 4 RC was necessary (I only have v3 RTM).

  • Dan,

    I have a question about your example. You are not using a specific class as a model to simplify the example?
    If you would use a class, for example MainPageModel, how it would load it's data?
    With methods as members of the model class but calling them from the viewmodel or with methods members of the viewmodel, calling them from the viewmodel and then assigning the result to the model.
    Thanks,
    Fer

  • Fer,

    It's all personal preference but I think that the model classes (at least on the client-side) should simply be data containers. The ViewModel is quite similar to a controller so it should be responsible for getting all the data, interacting with services, etc. While you can put all the methods to retrieve and modify data in the ViewModel classes I like to separate that out into a ServiceAgent class. That way the methods that get data are re-usable across multiple ViewModels if needed.

    Dan

Comments have been disabled for this content.