Search code examples
wpfmvvmuser-controlsmvvm-light

MVVM-Light and WPF User control library


I will have to create a WPF user control in a stand alone user control library. I want to use MVVM-light. Question, the SimpleIOC is normally setup in the Main application and added as a resource to the Main application through app.xaml.

How do I best solve it such that the referenced user control library get access to the SimpleIOC container? Where do I best register the User control's ViewModel(s)?

I found the following thread that seems to be the same question, but there is no longer any information about the solution in the link provided in the answer post. How can MVVM Light be used in a WPF User Control Library project?


Solution

  • If the views in the stand-alone class library have a dependency upon a view model locator which in turn has dependencies upon the view model classes, you could define the ViewModelLocator class as a singleton in the view model project and reference this project from both the User Control library and the WPF application itself:

    enter image description here

    ViewModels/ViewModelLocator.cs:

    public sealed class ViewModelLocator
    {
        private static readonly ViewModelLocator _instance = new ViewModelLocator();
    
        private ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<MainViewModel>();
        }
    
        public static ViewModelLocator Instance => _instance;
    
        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }
    }
    

    Views/UserControl1.xaml:

    <UserControl x:Class="Views.UserControl1"
                 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:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:viewModels="clr-namespace:ViewModels;assembly=ViewModels"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300"
                 DataContext="{Binding Main, Source={x:Static viewModels:ViewModelLocator.Instance}}">
        <Grid>
    
        </Grid>
    </UserControl>
    

    WpfApp4/MainWindow.xaml:

    <Window x:Class="WpfApp4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:views="clr-namespace:Views;assembly=Views"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <views:UserControl1 />
        </Grid>
    </Window>