在单独的项目中定义 WPF 资源时避免 Visual Studio 设计器错误 [英] Avoiding Visual Studio designer errors when WPF resource is defined in separate project

查看:19
本文介绍了在单独的项目中定义 WPF 资源时避免 Visual Studio 设计器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单独的项目中定义 WPF 资源时,如何避免 Visual Studio 设计器错误?

How can I avoid Visual Studio designer errors when a WPF resource is defined in separate project?

我在一个复合 WPF 应用程序中有三个项目:主应用程序、一个基础设施"库和一个模块"库.主应用程序通过其他项目的输出 DLL 引用其他项目(这些项目不一起位于单个解决方案中).

I have three projects in a composite WPF application: the main application, an "infrastructure" library, and a "module" library. The main application references the other projects via their output DLLs (the projects are not located in a single solution together).

我正在基础设施"库中定义皮肤(ResourceDictionary 中的一些画笔和样式).我希望主应用程序选择一个皮肤并使其可用于整个应用程序(通过 App.xaml 中的 MergedDictionaries).

I am defining a skin (some brushes and styles in a ResourceDictionary) in the "infrastructure" library. I would like the main application to select a skin and make it available to the entire application (via MergedDictionaries in App.xaml).

在我的模块中,我想使用主应用程序加载的皮肤中定义的资源.如果我像这样引用资源,就好像它在本地可用一样:

In my module I want to use the resources defined in the skin that the main application loads. If I reference the resource as if it were locally available like this:

Background={StaticResource MainBackgroundBrush}

几乎一切都按预期工作.例外是 Visual Studio 的设计者感到困惑并告诉我未找到静态资源引用‘MainBackgroundBrush’".这有效地阻止了我使用设计器.

almost everything works as desired. The exception is that Visual Studio's designer gets confused and tells me that "StaticResource reference 'MainBackgroundBrush' was not found". This effectively prevents me from using the designer.

如何在项目中定义皮肤"ResourceDictionary,在主应用程序中引用该皮肤,然后在模块项目中使用其资源?

What can I do to define a "skin" ResourceDictionary in a project, reference that skin in the main application, and then use its resources in a module project?

推荐答案

一种可能的解决方案是使用 DynamicResource 而不是 StaticResource.Visual Studio 2008 设计器只显示控件而没有任何样式,就像 VS2010 beta 1 在无法解析 StaticResource 时所做的那样.

One possible solution is to use DynamicResource rather than StaticResource. The Visual Studio 2008 designer simply displays the controls without any styling, like VS2010 beta 1 does when it cannot resolve a StaticResource.

使用 DynamicResource 适用于特定样式可能在运行时更改的情况,例如换肤时.

Using DynamicResource is appropriate in situations where a particular style may change at runtime, like when skinning.

我发现了一些支持此观点的相关问题:

I found some related questions supporting this:

我还发现有人说,只要资源不是本地的,就应该使用 DynamicResource:

I also found someone who states that DynamicResource should be used whenever a resource is not local:

这篇关于在单独的项目中定义 WPF 资源时避免 Visual Studio 设计器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆