UWP ResourceDictionary does not load when I add a second object to it

I have a UWP app for Windows 10 with MVVM Light enabled. I store ViewModelLocator in App.Resources. When I have only ViewModelLocator in my App.Resources, everything works fine.

<Application.Resources> <viewModel:ViewModelLocator x:Key="Locator" /> </Application.Resources> 

As soon as I add String, Converter or something similar, the application does not crash, but the ViewModelLocator constructor is no longer called. Errors and exceptions do not occur, only ResourceDictionary does not load or does not work at boot time.

 <Application.Resources> <viewModel:ViewModelLocator x:Key="Locator" /> <x:String x:Key="SampleString">Hello</x:String> </Application.Resources> 

If I add a style, DataTemplate, Brush, Color - everything will be fine.

Have not noticed this behavior on Windows Phone 8, Silverlignt or WPF before. Moving styles or objects to separate ResourceDictionaries and loading them with MergedDictionaries did not help.

I would like to have a list of objects in a ResourceDictionary so that all the constructors of these objects are called automatically when the application starts. Please inform.

PS :. Even two of these converters do not work, and one is created without any problems.

 <Application.Resources> <!--<viewModel:ViewModelLocator x:Key="Locator" />--> <converters:StringFormatConverter x:Key="StringFormat1" /> <converters:StringFormatConverter x:Key="StringFormat2" /> </Application.Resources> 

We are looking for an example of using ResourceDictionary, I found a similar question: Unified ResourceDictionary initialization in a UWP application

+2
source share
2 answers

This is all due to lazy initialization. I did some experiments and understood this picture. I hope you understand this idea. http://screencast.com/t/mxyBGBDuZ

+1
source

Try adding them to the resource dictionary, for example

 <Application.Resources> <ResourceDictionary> <viewModels:ViewModelLocator x:Key="Locator"/> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="XAMLResources/Styles.xaml" /> <ResourceDictionary Source="XAMLResources/DataTemplates.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

If you want to take a look at the full example, a snippet of code from this repository on GitHub https://github.com/AppCreativity/Kliva

You will notice that we added our Converters to Styles.xaml and it works great ...

+3
source

Source: https://habr.com/ru/post/1238517/


All Articles