【问题标题】:Global static resources in a WPF class library?WPF 类库中的全局静态资源?
【发布时间】:2010-06-25 05:36:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: wpf xaml wpf-controls mvvm-light


    【解决方案1】:

    您可以使用您的资源创建一个 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);
    

    【讨论】:

    • 您可以在 XAML 中的 App.xaml 文件中创建 MergedDicionaries,这是最常见的做法(而不是在后面的代码中创建它们)。
    猜你喜欢
    • 2011-06-14
    • 2013-10-23
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多