During the development of my application for UWP, I noticed and became interested in a strange thing, which is difficult for me to explain.
I am a MvvmLight user, and I decided to add an instance of the ViewModelLocator resource to a separate ResourceDictionary Core.xaml, which will be referenced by MergedDictionaries in App.xaml. The following is the contents of App.xaml:
<Application ...> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/Core.xaml" /> <ResourceDictionary Source="Resources/Converters.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
Core.xaml Content:
<ResourceDictionary ...> <viewModel:ViewModelLocator x:Key="Locator" /> </ResourceDictionary>
Now I assumed that resources in Core.xaml are initialized when I call the InitializeComponent method in App.xaml.cs, but when I tried to use the ServiceLocator class (which is set in the ViewModelLocator constructor in MvvmLight) - like this - ServiceLocator.Current.GetInstance<INavigationService>().Navigate<MainViewModel>(); - I get an exception:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Practices.ServiceLocation.dll but was not handled in user code Additional information: ServiceLocationProvider must be set.
In fact, if I set a breakpoint in the ViewModelLocator constructor, it is not called before the window is activated. Even more interesting: if I manually refer to the Locator resource key (for example, by placing Debug.WriteLine(Resources["Locator"]); on a call to ServiceLocator ), everything works fine. The same thing happens if I move the ViewModelLocator resource directly to App.xaml - then it is created with IntializeComponent .
Is there lazy creation of unified resource dictionaries in UWP applications? Or why does he behave this way?
source share