Eager Instantiation of ViewModels with MVVM Light Messaging

Dependency registration in SimpleIoc, by default uses Lazy Instantiation, meaning the object won’t be created until it is requested, in this case, via the property on the ViewModelLocator class.

Due to configuring subscription of a message type in the Constructors of my ViewModels in MVVM Light I recently came across a situation whereby messages were being published prior to them having been subscribed to, meaning that the messages disappeared into the void unhandled.  The ViewModels relating to the pages I had navigated to were available in the ServiceLocator, but the pages that I hadn’t navigated to were not being instantiated.

Continue reading “Eager Instantiation of ViewModels with MVVM Light Messaging”

Universal App Gotchas : Namespaces must exist in all Platform Projects

In certain circumstances Visual Studio will quite happily suggest to add namespaces into Shared Projects that will then fail to compile.  It’s an obvious issue to resolve, with the bottom line being that the Namespaces must exist in all Platform Projects in order for it to be referenced by the Shared Project.
Continue reading “Universal App Gotchas : Namespaces must exist in all Platform Projects”

Universal App Gotchas : NullReferenceException in Blend Opening Views from Visual Studio with MVVM Light

TL;DR: open the whole solution in blend*.

*for my scenario.

On using the Open In Blend menu option from right clicking a view in Visual Studio…

OpenInBlend
Open In BlendBlendNullReferenceException.png

…I was experiencing the following System.NullReferenceException when opening the view in the Designer…

BlendNullReferenceException.png
System.NullReferenceException

…on further investigation “The Resource “Locator” Could not be resolved.”

Explanation

When you right click Open in Blend, it will open the project that your view resides in, along with the View itself in the design window. What it won’t do however, is bring with it any resources it requires from other projects at design time. In this case, my App.xaml file located within the .Shared project, and NOT the platform specific project being loaded was defining my static resources and as such it could not be resolved due to not currently being loaded in Expression Blend. As mentioned in a previous post, the shared folder for all intents and purposes is part of the platform specific projects at compile time.

 Resolution

My resolution was simple, open the entire Solution in Blend allowing it to be resolved!

Seems obvious now but I was stumped for a little while there! Hopefully this will come in handy to someone.