I have been working fine with MVVM Light on a silverlight 4 Windows Phone 7 project in Visual Studio 2010 and Blend 4 for sometime.
Then suddenly I start getting the “Cannot create an instance of “ViewModelLocator”” error in both VS 2010 and Blend. Can’t work out why it should appear now. Any ideas on how I can possibly track down if something somewhere has changed that I have not spotted.
The only change I made, and then put back, was to add a “d:” infront of the data context expression in one of the user controls. I had the idea of binding in design, but programmatically binding in a delayed manner at run time.
I see others have posted this problem, but the answers refer to a bug in Blend, which has apparently been fixed. Also this is occuring in VS2010 too.
App.xaml looks like
<Application x:Class="BillSplitter2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:vm="clr-namespace:BillSplitter2.ViewModel"
xmlns:converters="clr-namespace:HardMediumSoft.WP7.Tools.Converters;assembly=HardMediumSoft.WP7.Tools">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/ResourceDictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>
<!--Localization String Library-->
<local:LocalizedStrings xmlns:local="clr-namespace:BillSplitter2.Utilities"
x:Key="LocalizedStrings" />
<!--Converters -->
<converters:FloatConverter x:Key="FloatConverter" />
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<converters:StringToBrush x:Key="StringToBrushConverter" />
</ResourceDictionary>
</Application.Resources>
<Application.ApplicationLifetimeObjects>
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService Launching="Application_Launching"
Closing="Application_Closing"
Activated="Application_Activated"
Deactivated="Application_Deactivated" />
</Application.ApplicationLifetimeObjects>
Thanks to the recommendation above, I started to investigate the constructor of my ViewModel. Although I had no errors, I did find that belnd had issues with event listeners and handlers.
I was using the
to populate some values for design time. I would initiate values in the model by setting their properties. This was all fine until I started adding more complex event handling routines based on property changes.
To rectify this and recover my ‘blendability’ I did two things!
Hope this helps!