MVVM Light ViewModelLocator

I am using MVVM Light in my application with several assemblies in the solution. How can I have a ViewModelLocator in each assembly without a definition in App.xaml resources?

Typically, a ViewModelLocator is defined as follows:

<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Sol1;component/GlobalResources.xaml" /> </ResourceDictionary.MergedDictionaries> <viewModels:ViewModelLocator xmlns:main="clr-namespace:MainModule.ViewModel;assembly=MainModule" x:Key="Locator" d:IsDataSource="True"/> </ResourceDictionary> 

I created Resource.xaml in the class library in my solution as follows:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ModuleX="clr-namespace:ModuleX"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Common;component/CommonResources.xaml" /> </ResourceDictionary.MergedDictionaries> <ModuleX:ViewMoldelLocator x:Key="ModuleXLocator" /> </ResourceDictionary> 

However, ModuleXLocator was not found:

 <phone:PhoneApplicationPage ... DataContext="{Binding Main, Source={StaticResource ModuleXLocator}}"> 
+6
source share
1 answer

I am not 100% if I answer your question, but I will answer the similar problem that I had and its solution. Hope this is what you are looking for.

Problem:

You have developed the wonderful MVVMLight application (LittleApp), and now you want to use it as a UserControl / Page / Window in another project (BigApp). The problem is that LittleApp has its own ViewModelLocator defined in the corresponding App.xaml, which is not used for BigApp. As a result, LittleApp searches its ViewModelLocator in "BigApp App.xaml" and fails.

You also want LittleApp to work as a standalone project, and therefore do not want to interfere with its design.

Provides possible exceptions:

XMLParseException: ViewModelLocator defined in the root directory of LittleApp node cannot be found (because it is defined in an unused App.xaml)

WPFError 40: you used the MVVMLight tool to create both applications, and thus both applications have an App.xaml with a ViewModelLocator called "Locator" pointing to the ViewModel called "Main". LittleApp will be able to resolve this, but it will point to the wrong MainViewModel and, therefore, to property conflicts.

Decision:

You need to define LittleApp ViewModelLocator as a resource in the root directory of node MainA. Therefore, if your MainView is a UserControl called LittleApp / View / MainView.xaml, it will look something like this.

 <UserControl x:Class="LittleApp.View.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:LittleApp.ViewModel" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="../Skins/MainSkin.xaml" /> </ResourceDictionary.MergedDictionaries> <vm:ViewModelLocator x:Key="Locator"/> </ResourceDictionary> </UserControl.Resources> <UserControl.DataContext> <Binding Source="{StaticResource Locator}"/> </UserControl.DataContext> <Grid> . . Your Control Design . . . </Grid> </UserControl> 

Now BigApp just needs to reference the LittleApps assembly and add it as the namespace in the root directory of the control node to which you want to add it. Something like that.

 <Page x:Class="BigApp.View.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:LittleView="clr-namespace:LittleApp.View;assembly=LittleApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <LittleView:MainView/> </Grid> </Page> 
+5
source

Source: https://habr.com/ru/post/971937/


All Articles