In a WPF application, you can put your global static resources in app.xaml .. like
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
</Application.Resources>
That was from MVVM Light ;). Now, if your project is a wpf class library, what is a proper way to initialize such global static resources?
Static Resource - Static resources are the resources which you cannot manipulate at runtime. The static resources are evaluated only once by the element which refers them during the loading of XAML.
WPF supports different types of resources. These resources are primarily two types of resources: XAML resources and resource data files. Examples of XAML resources include brushes and styles. Resource data files are non-executable data files that an application needs.
In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology.
Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.
You can create a ResourceDictionary with your resources and merge the dictionaries using your code as below.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:vm="clr-namespace:WPFProject.ViewModel"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
Code:
Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(
new Uri("/WPFProject;Component/Resources/ResourceDictionary1.xaml", UriKind.Relative)) as ResourceDictionary);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With