Tejas Patel
Tejas Patel

Reputation: 1447

Do we have to create induvidual ViewModelLocators for each ViewModel?

Do we have to create induvidual ViewModelLocator for Different ViewModel or Only One ViewModelLocator for all ViewModels. Because When I try to use only one ViewModelLocator by making properties for all the ViewModels - It gives Error - Type Not Found in Cache. so m not able to use that viewmodel unless I set that viewModel as default. Can anyone please help me identify where I am going wrong??

Upvotes: 0

Views: 2147

Answers (3)

Dmitry Lyalin
Dmitry Lyalin

Reputation: 444

I caused this error in a Windows 8 store app when I used MVVM Toolkit just like you and had in my viewmodel's constructor the SimpleIoc container resolving types. This caused the design-time experience to show the error you describe.

I resolved it by checking if I was in design-time and not executing the code if I was

Upvotes: 0

Tejas Patel
Tejas Patel

Reputation: 1447

I found better solution and work around of MVVM-Light ViewModelLocator is to use MEF Export. Below is a good link by Johnpapa... Clean and Easy Solution. http://johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum

Upvotes: 1

vidalsasoon
vidalsasoon

Reputation: 4401

You only need one ViewModelLocator and need a setup similar to this:

You should have a ViewModelLocator.cs.

Have it initialized in your App.xaml:

 <Application.Resources>
    <ResourceDictionary>
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </ResourceDictionary>
</Application.Resources>

Also your Views should be calling your ViewModelLocator at the root DataContext of each page:

<phone:PhoneApplicationPage DataContext="{Binding MenuViewModel, Source={StaticResource Locator}}" .../>

Upvotes: 3

Related Questions