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

2

7

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>();
    }
}
Overcome answered 25/6, 2019 at 14:40 Comment(0)
A
1

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.

Appassionato answered 25/6, 2019 at 16:17 Comment(3)
No I didn't had a clue what to put where, since the default in .net core 3 and VS Code is 0 scaffolding and documentation is lacking so you have to find it out.. by magic.. I've had a working version later today in the afternoon. But the ViewModelLocator you have looks a lot cleaner. When I'm back at the code I'll share some bits!Overcome
In the version I had the ServiceLocator class wasn't available anymore, also due to the version that is supported in .net core 3. Combining your feedback and the question I already found made sure I got it to work. Cheers! Thanks!Overcome
Glad I could help. Could you mark my suggestion as answer please, so that other people can follow this advice too. Thank you!Appassionato
H
4

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)

Hamhung answered 23/12, 2020 at 22:51 Comment(0)
A
1

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.

Appassionato answered 25/6, 2019 at 16:17 Comment(3)
No I didn't had a clue what to put where, since the default in .net core 3 and VS Code is 0 scaffolding and documentation is lacking so you have to find it out.. by magic.. I've had a working version later today in the afternoon. But the ViewModelLocator you have looks a lot cleaner. When I'm back at the code I'll share some bits!Overcome
In the version I had the ServiceLocator class wasn't available anymore, also due to the version that is supported in .net core 3. Combining your feedback and the question I already found made sure I got it to work. Cheers! Thanks!Overcome
Glad I could help. Could you mark my suggestion as answer please, so that other people can follow this advice too. Thank you!Appassionato

© 2022 - 2024 — McMap. All rights reserved.