Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start using MvvmLight on a .net core 3 WPF app?

For a new project I want to try out the new .net core 3.0 WPF project and I want to use it in combination with MvvmLight. However in .net core and in combination with Visual Studio Code you don't get any scaffolding or default project. And then there is the mystery what to do to get it working...

I know I need to do something in app.xaml.cs, mainwindow.xaml and mainwindow.xaml.cs. As well as creating some ViewModelLocator service. But the documentation of MvvmLight is kinda empty on this.

I had found the following question on SO (MvvmLightLibsStd10 and UWP), but it isn't complete in my case and I am also unsure whether I should use the normal package or the special std10 version.

Update 2019-06-26 I got it to work with the following code, using MvvmLightLibsStd10 version 5.4.1.1.

App.xaml

    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" xmlns:vm="clr-namespace:$AssemblyName$.ViewModel" />
        </ResourceDictionary>
    </Application.Resources>

MainWindow.xaml

        DataContext="{Binding ValidatorListViewModel, Source={StaticResource Locator}}">

ViewModelLocator.cs

using GalaSoft.MvvmLight.Ioc;

namespace $AssemblyName$.ViewModel
{
    public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            SimpleIoc.Default.Register<ValidatorListViewModel>();
        }

        public ValidatorListViewModel ValidatorListViewModel => SimpleIoc.Default.GetInstance<ValidatorListViewModel>();
    }
}
like image 275
Paalders Avatar asked Jun 25 '19 14:06

Paalders


2 Answers

A lot of things have happened in the 1.5 years since you ask this question. Microsoft is now supporting a replacement for MVVMLight. Have you heard of Microsoft.Toolkit.MVVM? Please see: https://github.com/windows-toolkit/MVVM-Samples It is supposed to be more compatible with .NET Core. Opps, I forgot it is now just .NET (for Rev5 and over)

like image 110
MichaelInMA Avatar answered Oct 10 '22 09:10

MichaelInMA


What exactly do you mean by 'it isn't complete in my case'? Do you get any errors when building the project?

I'm using MvvmLight in my projects too. As an example:

In App.xaml

<ResourceDictionary>
                <vm:ViewModelLocator x:Key="Locator"
                                     d:IsDataSource="True"
                                     xmlns:vm="clr-namespace:$AssemblyName$.ViewModel" />
</ResourceDictionary>

In MainWindow.xaml

DataContext="{Binding Main, Source={StaticResource Locator}}"

In ViewModelLocator.cs

public ViewModelLocator()
{
   ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
   SimpleIoc.Default.Register<MainViewModel>();
}

public MainViewModel Main
{
     get
     {
         return ServiceLocator.Current.GetInstance<MainViewModel>();
     }
}

This should do the trick... But as mentioned above, it would be interesting to know, if you get any errors.

like image 27
Doern Avatar answered Oct 10 '22 07:10

Doern