.NET Standard 1.0 Support

This article below describes what .NET Standard is, how you can benefit from it, and how you can install MVVM Light into your .NET Standard based applications.

What’s .NET Standard?

In the past, when we wanted to share code between applications on multiple platforms, we had a few options: We could share files as links (shortcuts), which means that the same file was compiled multiple times, for instance once for WPF, once for Silverlight, etc. Differences between the platforms could be handled by using precompiler directives (for example “#if SILVERLIGHT”), which means that some of the code was ignored in WPF and active only in the Silverlight version of the binary. This was not very convenient, because precompiler directives can lead to pretty ugly code, difficult to read and to maintain.

Later, we got the option to use what was called Portable Class Libraries (PCL). This type of project created one binary that could be used on multiple platforms. Differences between the platforms were handled by selecting a profile. For example you could decide that your PCL was going to be used only on .NET 4.6, which would give you a lot of APIs. Or you could select .NET and Silverlight, and that would reduce the number of available APIs for your application. But the advantage is that you had compatible code, one binary only.

.NET Standard is, you could say, like portable class library on steroids. It allows to share binaries between platforms, not just on Windows but also Mac, Linux etc. Porting MVVM Light to .NET Standard 1.0 will allow users to create decoupled applications using the well known components on a larger number of platform than ever before.

Why 1.0?

I decided to shoot for the lowest version of .NET Standard possible, because this ensures the largest compatibility. For example at the time of writing, Universal Windows Platform applications (UWP) are not compatible with the latest version of .NET Standard (2.0). However MVVM Light for .NET Standard 1.0 is already compatible with UWP.

There is one little incompatibility between .NET Standard 1.0 and MVVM Light described below (under Known issues). Please take note that converting an existing MVVM Light application to use .NET Standard will require minor changes as shown below.

How to install?

In a .NET application, UWP application, Xamarin.iOS or Xamarin.Android application

The easiest way is to use the Nuget package manager.

If you had MVVM Light installed already:

First you need to remove the Portable Class Library version of MVVM Light:

  • Open the application in Visual Studio
  • Open the Nuget package manager
    • Right click on the Solution
    • Select Manage Nuget Packages for Solution
  • In the Nuget package manager, select the MvvmLightLibs package.
  • In the Versions box on the right, select every project that has MvvmLightLibs installed.
  • Click on Uninstall.
  • On the left, select the CommonServiceLocator package.
  • Again, in the Versions box, select every project that uses this package.
  • Click on Uninstall.

2017-09-03_22-48-46
(Click for larger version)

Adding MVVM Light for .NET Standard 1.0:

Then we will install the new MVVM Light for .NET Standard 1.0.

  • Select the Browse tab on top.
  • In the Search box, enter MvvmLightLibsStd10.
  • Make sure that the “Include prerelease” checkbox is checked.
  • In the search result list, select the package named MvvmLightLibsStd10.
  • Select all the projects that need this package.
  • Click on Install.
  • Accept the license.
2017-09-03_23-01-37
(Click for larger version)

Note that you can also install MVVM Light for .NET Standard 1.0 from the Nuget console directly with the following directive:

Install-Package MvvmLightLibsStd10 -Version 5.4.0.1-alpha 

Or from the .NET command line interface:

dotnet add package MvvmLightLibsStd10 --version 5.4.0.1-alpha 

This process adds two DLLs to your new application:

  • GalaSoft.MvvmLight.dll contains all core components compatible with .NET Standard.
  • GalaSoft.MvvmLight.Platform.dll contains platform-specific components such as the NavigationService, DialogService etc.

There is no more GalaSoft.MvvmLight.Extras.dll. This assembly is not needed in the .NET Standard version anymore.

Note that there is a small incompatibility between the current version of MVVM Light and the new .NET Standard version. See below the Known issue section for details.

In a .NET standard library

You can also add MVVM Light to a .NET Standard class library, for example if you need the MVVM Light components in your own library. In order to add MVVM Light, simply use the Nuget package manager and make sure that you have the Prerelease checkbox checked. Adding MVVM Light to your .NET Standard class library only requires one DLL named GalaSoft.MvvmLight.dll.

Known issues

Unfortunately, it was not possible to port MVVM Light as is. One particular interface, IServiceProvider, which is used by the SimpleIoc component, is not available in .NET Standard 1.0. After some thinking and consulting with the .NET team, I decided to remove this interface from MVVM Light .NET edition. This requires some changes in your application code, as described below.

Note: If you continue to use the portable class library version of MVVM Light, nothing changes. This only concerns the .NET Standard 1.0 version of MVVM Light, contained in the Nuget package called MvvmLightLibsStd10.

The IServiceProvider interface was used by the ServiceLocator class (from the CommonServiceLocator Nuget package). This class was used historically to allow developers to easily switch from one IOC container to another without having to modify multiple locations. This sounded like a great idea but in recent years, this strategy raised a few concerns. It added a dependency on an external library (CommonServiceLocator) which was not actively maintained. When new platforms (such as Windows 8.1 or Windows 10) were released, porting the CommonServiceLocator took some time, which in turn delayed the release of the dependent libraries.

In MVVM Light for .NET Standard 1.0, I decided to remove this dependency completely. As a result, it means that the ServiceLocator class is not available anymore.

For an existing application, this means the following changes (old code commented out).

VIEWMODELLOCATOR.CS

"Using" section

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
// OLD using Microsoft.Practices.ServiceLocation;
using MvvmLightWithPcl.Model;

Constructor

static ViewModelLocator()
{
    // OLD ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    // Rest of the constructor unchanged

}

MAINVIEWMODEL.CS AND OTHER LOCATIONS:

In the rest of the code, you need to remove every mention to ServiceLocator.Current and replace with SimpleIoc.Default. For example:

// OLD var nav = ServiceLocator.Current.GetInstance<INavigationService>();
// NEW
var nav = SimpleIoc.Default.GetInstance<INavigationService>();

Support

This is a pre-release version and I am eager to hear your feedback. If you have any issue during the uninstallation process, installation process or while using MVVM Light for .NET Standard 1.0, send me an email at Laurent@galasoft.ch for assistance.

Happy coding!
Laurent