This week I published MVVM Light V5.2 to Nuget and to the Visual Studio Gallery.
There are a few highlights which are described as always in the Change Log. See further below for a more detailed explanation of some of the changes.
Using Nuget to install MVVM Light
It is possible to add MVVM Light to an existing application using the Nuget package manager. This can be done over command line, or using the UI included in Visual Studio. There are two packages to consider when selecting MVVM Light in an existing application: MVVM Light “libraries only” (mvvmlightlibs) and MVVM Light “full package” (mvvmlight) which also adds some files and scaffolding to the application.
Installing using Visual Studio
In order to install either MVVM Light package using Visual Studio, follow the steps:
- Open your existing application in Visual Studio.
- In the Solution Explorer, right click on the project you want to add the references to, and select Manage Nuget Packages from the context menu.
Note: You can also manage Nuget packages in multiple projects by right-clicking on the Solution and selecting Manage Nuget Packages for Solution.
- In the Nuget package manager window, make sure that Package source is set to Nuget.org. Set Filter to All, and Search for “mvvmlight”.
- Select either MvvmLight or MvvmLightLibs, and click on the Install button.
Note: using the Nuget package manager window, you can also remove MVVM Light from your application later, if needed, or upgrade to a newer version.
This action installs the selected package in your application.
If you select the “mvvmlightlibs” package, this installs the GalaSoft.MvvmLight assemblies in your application. In addition, it installs the CommonServiceLocator package, which is a reference needed by MVVM Light. No other changes are made to the application.
Note: if you add MVVM Light to a class library, you should use the “mvvmlightlibs” package only!
If you select the “mvvmlight” package, it installs the “mvvmlightlibs” package to your application, and in addition adds two files to your project:
If your application is a Windows XAML-based app, it also executes a script to modify the App.xaml to add the ViewModelLocator to the application in a way that makes it possible to use design time data in Blend.
Note: The script is only executed in applications containing an App.xaml. In Xamarin.Android, Xamarin.iOS and Xamarin.Forms, the script doesn’t modify the application.
Caution: Full package in Windows 10 Universal applications (UWP)
There is a known issue when you install the “mvvmlight” package in Windows 10 universal applications. The Nuget team unfortunately changed the way that Nuget works for this framework, and it doesn’t allow installing additional files, or running scripts anymore. Because of this, installing the “mvvmlight” package in Windows 10 UWP applications creates the exact same result as installing the “mvvmlightlibs” package. Because running a script is not allowed, it is not even possible to warn the user.
I will publish more detailed information on how to add the scaffolding to a Windows 10 UWP application manually. Stay tuned.
Caution: Portable.MvvmLightLibs is discontinued
If you search in Nuget for “mvvmlight”, you may notice a package named “portable.mvvmlight”. This package’s description mentioned that it is discontinued. Use the “mvvmlightlibs” package instead, which is also featuring portable class libraries, and is actively maintained.
Using the Visual Studio extension VSIX
If you wish to start with a fresh new template with all the scaffolding in place, and in addition get code snippets right into Visual Studio, then the VSIX is for you!! This is a simple installer that will install MVVM Light templates and snippets into your favorite IDE. Currently, we support Visual Studio 2012, 2013 and 2015.
Installing from the Visual Studio gallery
The easiest way to install the MVVM Light extension is directly from within Visual Studio with the following steps:
- Open the menu Tools / Extensions and Updates.
- In the Extensions and Updates windows, select Online on the left.
- Select Visual Studio Gallery.
- Enter the search string “mvvmlight” in the search box.
- Select the MVVM Light extension and press Download.
- After installation, you need to restart Visual Studio (this is standard procedure for all VSIX installations).
Installing from Codeplex
If for some reason you don’t have access to the Visual Studio gallery, or if you want to save the VSIX file for offline installation, you can head to the MVVM Light Codeplex repository.
Make sure to download the correct VSIX for your favorite Visual Studio edition. Currently we support Visual Studio 2012, 2013 and 2015.
The new project templates
After you install MVVM Light, you will immediately see project templates in the File, New Project dialog. Creating interesting project templates is a challenge, because you also don’t want to overload the “empty template” with too much stuff that needs to be deleted. However a project template is also a good place to add a few simple samples to teach the basics to the new users.
In this spirit, I created new project templates for Windows 10 (Universal), Xamarin.iOS, Xamarin.Android and Windows Phone 8.1 with a set of features that demonstrates how the components of MVVM Light can be used. It is not intended as a complete tutorial (please head to the Pluralsight course for that), but it should give you a good idea of how MVVM Light can enhance your application.
The new project templates for Windows Phone 8.1, Windows 10, Xamarin.iOS and Xamarin.Android feature most of the MVVM Light components and a small sample for each of these components.
Note: I will gradually update the project templates for other platforms too.
The user interface looks like this:
Application in Windows 10 (desktop)
The application features the following features:
- Standard MVVM Light scaffolding: ViewModelLocator, MainViewModel
- Design mode: In ViewModelLocator and DesignDataService
- ViewModelBase and PropertyChanged (with the Set method): In MainViewModel.cs
- Databinding in MainPage.xaml
- RelayCommand: In MainViewModel.cs
- Databinding in MainPage.xaml
- Initialization in App.xaml.cs (Win10 and WinPhone), App.cs (Android), AppDelegate.cs (iOS)
- Usage in MainViewModel (RunClock method)
- Registration in App.xaml.cs (Win10 and WinPhone), MainActivity.cs (Android), AppDelegate.cs (iOS)
- Sending message in MainViewModel.cs
- Initialization in ViewModelLocator
- Resolution in MainViewModel.cs
- Configuration in ViewModelLocator
- Navigation with parameter from MainViewModel
- Back navigation, Retrieving parameter in SecondPage (Win10 and WinPhone), SecondActivity (Android), SecondViewController (iOS)
- Registration in ViewModelLocator
- Usage in MainViewModel
Design mode: see DesignDataService and ViewModelLocator
Android, iOS, Windows Phone 8.1, Windows 10
The new template is available for Windows 10 Universal, Windows Phone 8.1, Xamarin.Android and Xamarin.iOS. In addition, there are still the “old” template available for Windows Phone 8.0 and 8.1 Silverlight, Windows 8.1, WPF, Silverlight 5.
Application in Windows 10 (phone)
Application in Windows Phone
Application in iOS
Application in Android
The Code Snippets
Like in previous versions, the Visual Studio extension is also installing code snippets. You can see the list of code snippets by placing the cursor anywhere in the code, and typing “mvvm”.
The following code snippets are available:
- mvvmdroidelement (Xamarin.Android only): A property with backing field exposing a UI element retrieved from the resources.
- mvvmformsbprop (Xamarin.Forms only): Create a Bindable Property for Xamarin Forms. See below for details.
- mvvminpc* (multiple variants): A property raising PropertyChanged with a string. The class using this property should inherit GalaSoft.MvvmLight.ObservableObject.
- mvvmlocatorproperty: A ViewModel property to be added to a GalaSoft.MvvmLight.ViewModelLocator class.
- mvvmpropa: Registers an Attached DependencyProperty (WPF).
- mvvmpropdp: Registers a DependencyProperty (WPF).
- mvvmrelay* (multiple variants): Creates and initializes a new RelayCommand
- mvvmslpropa: Registers an Attached DependencyProperty (Silverlight).
- mvvmslpropdp: Registers a DependencyProperty (Silverlight).
- mvvmvm: A property allowing easy access to a view’s DataContext.
A few new features in the spotlight
V5.2 fixes quite a number of issues reported by users. In addition, the two following features are new:
DispatcherHelper in Android and iOS
DispatcherHelper is a convenient helper class that can be used to “dispatch” instructions from a background thread to the main thread. This is needed when, for example, a UI element needs to be updated from a worker thread. This is also the case if a worker thread updates a property which raises the PropertyChanged event, and a databinding is connecting this property to a UI element’s property (for example a TextBlock.Text property).
In the new project templates in Windows 10 Universal, Windows Phone 8.1, Xamarin.Android and Xamarin.iOS, this is demonstrated by implementing a clock. A worker thread is started in the MainViewModel’s RunClock method. This worker thread updates the Clock property (of type string) every second. Because a databinding is set between the Clock property and a label on the user interface, the DispatcherHelper needs to be used.
In V5.2, this class is made available to the Xamarin.Android and Xamarin.iOS frameworks too (it was already available in the XAML-based frameworks before).
Note: At this time, DispatcherHelper cannot be used in a portable class library. I am working on a solution.
Xamarin.Forms “bindable property” code snippet (mvvmformsbprop)
In Xamarin.Forms, a concept similar to XAML’s DependencyProperty exists under the name “BindableProperty”. Declaring a BindableProperty is tedious and repetitive. This is why I made a code snippet available in V5.2 which helps the developer to declare a new BindableProperty. This snippet is similar to the previously existing
Hopefully you will find that MVVM Light is as useful in Windows 10 universal applications than in all other supported frameworks. With Windows 10 and Xamarin support, the range of devices that your applications can reach is greater than ever (I cannot wait to see MVVM Light applications running on HoloLens for instance