问题
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