WPF 类库中的全局静态资源?

发布于 2024-09-07 17:27:06 字数 359 浏览 1 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-09-14 17:27:06

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文