Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global static resources in a WPF class library?

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?

like image 255
amazedsaint Avatar asked Jun 25 '10 05:06

amazedsaint


People also ask

What is static resource in WPF?

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.

What default resources are supported in WPF?

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.

What can we store in a ResourceDictionary?

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.

How do I add resources to XAML dictionary?

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.


1 Answers

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);
like image 92
Ragunathan Avatar answered Sep 28 '22 22:09

Ragunathan