UWP / MVVMlight : Replacing Obsolete ServiceLocator and SimpleIoc Register

只谈情不闲聊 提交于 2019-12-11 17:45:31

问题


I open this topic again.

I face a probelm when I update my UWP app: I'm gonna give more detail: Here is my old code before update running perfectly on UWP/Win10 10240 in my app.xaml

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

in my App.xaml.cs

public class ViewModelLocator
{
    public const string MainMenuPageKey = "MainMenuPage";
    public const string QuestionPageKey = "QuestionPage";

    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        var nav = new NavigationService();
        nav.Configure(MainMenuPageKey, typeof(MainMenuPage));
        nav.Configure(QuestionPageKey, typeof(QuestionPage));

        SimpleIoc.Default.Register<INavigationService>(() => nav);
        SimpleIoc.Default.Register<IDialogService, DialogService>();
        SimpleIoc.Default.Register<IDataService, DataService>();

        SimpleIoc.Default.Register<MainMenuViewModel>();

    }



    public MainMenuViewModel MainMenu=>
            ServiceLocator.Current.GetInstance<MainMenuViewModel>();

in my vm:ViewModelLocator.cs:

I want to update to UWP/Win10 16299 as servicelocator has disappear, here is the code I use:

public const string MainMenuPageKey = "MainMenuPage";
    public const string QuestionPageKey = "QuestionPage";

    static ViewModelLocator()
    {          
        var nav = new NavigationService();

        nav.Configure(MainMenuPageKey, typeof(MainMenuPage));
        nav.Configure(QuestionPageKey, typeof(QuestionPage));


        SimpleIoc.Default.Register<INavigationService>();
        SimpleIoc.Default.Register<IDialogService, DialogService>();
        SimpleIoc.Default.Register<IDataService, DataService>();

        SimpleIoc.Default.Register<MainMenuViewModel>();

    }

    public MainMenuViewModel MainMenu => 
       SimpleIoc.Default.GetInstance<MainMenuViewModel>();

it looks like 'register' creates the crash

Do you have any trick to solve this?

Regards;


回答1:


It's OK to use SimpleIoc directly rather than use it through ServiceLocator. Most of your code are correct. The only problem is this line:

SimpleIoc.Default.Register<INavigationService>();

It should be:

SimpleIoc.Default.Register<INavigationService>(() => nav);

So you don't give a specific type for INavigationService. You can add a try-catch for that line to get the exception details.




回答2:


Thanks for your help.

Actually to solve it, I create dynamically an instance of the ViewModelLocator in my app.xaml.cs. Then I put a breakpoint on the instance. The system crashed because of the ServiceLocator library I've downloaded in nuget.

Once this Dll is deleted, everything runs fine.



来源:https://stackoverflow.com/questions/47520508/uwp-mvvmlight-replacing-obsolete-servicelocator-and-simpleioc-register

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!