Friday, November 1, 2013

MvvmLight: Sharing code between WPF and Windows Store app using Portable Class Library (PCL)

There is a great blog write up setting up a project to share code via a PCL so the code can be used in MvvmLight projects targeting Win8 and Windows Phone.

I am attempting the same thing with a Win Store app (Win8) and a WPF45 app.

 Following along the tutorial, I get the WPF version working.

The Store version has this funky error:

Error 1 Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The assembly version has a public key token that does not match that of the request. (Exception from HRESULT: 0x80132001) Visual Studio 2012\Projects\MvvmPCLExample\Mvvm.Store\App.xaml 13 13 Mvvm.Store

If I run the project, it throws an exception that it cannot locate ViewModelLocator.

The problem was the store pointing to the PCL Version of Microsoft.Practices.ServiceLocation.

Fix

  1. Store project, References, Remove Microsoft.Practices.ServiceLocation 
  2. Add Reference, Browse  
  3. Under the project directory find packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll and add that. Rebuild and it runs.


Although Visual Studio still reports the 'Could not load file or assembly error...'. But it doesn't seem to cause a problem.

UPDATE: After rebooting my computer, the error is totally gone! Reminds me of the joke about fixing a car made my Microsoft ;)

Sample Project if anyone wants to take a look.

Windows 8.1
Visual Studio 2012
MvvmLight 4.2.30