问题
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