7

在 WPF 应用程序中,您可以将全局静态资源放在 app.xaml 中。

 <Application.Resources>
        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />
    </Application.Resources>

那是来自 MVVM Light ;)。现在,如果您的项目是 wpf 类库,那么初始化此类全局静态资源的正确方法是什么?

4

1 回答 1

4

您可以使用您的资源创建一个 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);
于 2010-06-25T07:29:59.427 回答