background preloader

Liberty MVVM Light Series

Facebook Twitter

Passing Parameters With Behaviors In MVVM Light for Windows Phone. Windows Phone From Scratch #20 MVVM Light Toolkit Soup To Nuts #5 In the previous posting in this series, we created a list of customers and we bound them to a list box. We did this not in the code-behind but in the View-Model. This forces the question of how we respond when a user makes a selection in the list box, if the logic for handling the selection is to be not in the view (the code-behind) but in the view-model.

The answer will not be overly surprising to anyone who has been following this series; we’ll use a behavior to capture the selected value and pass the selection to the view model class. The trick here is to use RelayCommand<T> rather than a simple RelayCommand. Taking this step by step, please re-open the project and create a new MVVMLight View named Details. Now, taking the lesson from Part 2, open MainViewModel.cs and add a property for the command (this will be bound to by the behavior) In the constructor initialize the RelayCommand: Complete source is available here.

Windows Phone From Scratch #19 – MVVM Light Toolkit Soup To Nuts #4. Let’s back up a bit and examine the day to day use of a View Model, and binding to the view model. In this mini-tutorial I’ll show the basics of binding a collection that sits in a View Model to a list box in the view. In the next, I’ll show how to capture the selection and, in the view model, determine what the details page should show.

Let’s create a simple application that will display the full name of customers and their email, and that will (eventually) allow the user to click on one and go to the details about that customer. We start by creating a new MVVM application for Windows Phone. Once created, open the application in Expression Blend and add a ListBox to the content panel. Creating the CustomerCollection The CustomerCollection class exists to give us some data to work with. Binding the ItemSource The key to understanding how MVVM handles the data binding is that the DataContext for the View is the View Model.

The complete source code for this mini-tutorial is available here. Windows Phone From Scratch #18 – MVVM Light Toolkit Soup To Nuts 3. This is the third part of the MVVM Light Toollkit Soup To Nuts (part 1 is here) within the Windows Phone From Scratch Mini-tutorial series. Today we look at an introduction to messaging as a tool for communicating, e.g., from the view-model to the view. What We’ll Build To illustrate this, we’ll return to the example we began in part 1, and extended in part 2 (you can download the part 2 source code here).

We stubbed out the handling of that command, but now it is time to complete the logic, by having the view model cause a navigation from MainPage to Page 2. There is and should be no visibility of a view from a view-model, however, and so we need a mechanism for sending out a message in a bottle to be picked up by anyone who is interested. Messaging Fortunately, the MVVM Light Toolkit provides extensive support for messaging. Begin by creating a new class in the project in Visual Studio, and call the new class GoToPageMessage. You’ll need to include the supporting library, Windows Phone From Scratch #17: MVVM Light Toolkit Soup To Nuts Part 2. This is the second part of the MVVM Light Toollkit Soup To Nuts (part 1 is here) within the Windows Phone From Scratch Mini-tutorial series. Today we look at an introduction to behaviors as a tool for migrating event handling from code-behind to the View-Model.

Behaviors were originally introduced in Blend to empower designers, but they turn out to be enormously helpful to C# programmers as well. For example, it is a design goal to move as much of the (testable) logic of the program out of code-behind (where testing is more difficult) and into the View-Model. But what to do about events, such as the click event on a button? What We’ll Build To illustrate this, we’ll return to the example we began in part 1, and extend it by adding a button to MainPage that will cause the application to navigate to page 2.

First we’ll substitute a behavior for the event handler Second we’ll use messages (in part 3) to tell the page to navigate to the new page Make the following changes to MainPage.xaml. (WPFS) MVVM Light Toolkit: Soup To Nuts Part I. While I’m not a zealot on the topic, I do believe that MVVM offers one of the best patterns for Windows Phone development and so, moving forward, the Windows Phone From Scratch series will incorporate MVVM in general, and the MVVM Light Toolkit in particular. I’m more than convinced that MVVM is an essential pattern for Windows Phone Development; and while there are many excellent frameworks to make MVVM development easier, the one I personally prefer to work with is the MVVM Light Toolkit and so it is the one I’ll focus on. I make the case for MVVM in this article, and so I won’t rehash that material here.

Instructions for installing MVVM Light Toolkit are available here. I will not assume any background with MVVM or with the Toolkit beyond those two links. That is, I’ll assume you’ve loaded the toolkit and created your very first, single page MVVM application, but not more than that. In that spirit, let’s begin by creating a two page MVVM application. Creating the Application Hey!