在 WPF 应用程序中,您可以将全局静态资源放在 app.xaml .. 中
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
</Application.Resources>
那是来自 MVVM Light ;)。现在,如果您的项目是 wpf 类库,那么初始化此类全局静态资源的正确方法是什么?
最佳答案
您可以使用您的资源创建一个 ResourceDictionary 并使用您的代码合并字典,如下所示。
<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" />
代码:
Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(
new Uri("/WPFProject;Component/Resources/ResourceDictionary1.xaml", UriKind.Relative)) as ResourceDictionary);
关于wpf - WPF 类库中的全局静态资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3115783/