Global static resources in a WPF class library?

空扰寡人 提交于 2019-12-22 05:39:10

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/3115783/global-static-resources-in-a-wpf-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!