WPF/Prism:如何使主应用程序中的资源渗透到单独程序集中包含的用户控件中?
我有以下内容:
MainApp - 程序集 1(包含 UserContolA) - 组件 2(包含 UserControlB) - 等等。
并不是每个程序集都会被加载,所以
如果我尝试将资源分配给它找不到的控件(按钮、组合框等),则两个程序集都无法编译,因为我希望我的 MainApp 来指定这些资源。但是,如果我将资源包含在程序集中,似乎我无法覆盖它们(因为 WPF 从下到上获取资源,优先考虑最接近底部的资源)。
另外,我担心即使我确实解决了这个问题,试图以用户可以从不同的“主题”中进行选择的方式使其变得动态,这也会使它变得更加困难。
我在主题中包含了棱镜,因为我计划使用它作为我的框架,但还没有足够深入地研究它是否会以任何方式影响它,或者已经内置了一些东西,这将是一个天赐良机。
I have the following:
MainApp
- Assembly 1 (Contains UserContolA)
- Assembly 2 (Contains UserControlB)
- Etc.
Not every Assembly will be loaded though, so
Neither assembly can compile if I try to assign resources to controls (button, combobox, etc.) that it can't find because I want my MainApp to dictate those resources. But, if I include the resources in the assemblies it seems that I can't override them (because WPF goes from bottom-up for resources giving prioroty to those closest to the bottom).
Also, I'm afraid even if I do solve this problem trying to get it to be dynamic in a way that a user can select from different "themes" that it'd make it even more difficult.
I included prism in the topic because I plan to use that as my framework, but haven't looked deep enough into it to see if it affects this in any way or has something built into it already, which would be a Godsave.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您要问的是将 WPF 应用程序拆分为多个程序集的最佳方法是什么。您的策略是创建可重用的控件库并让应用程序集成所有内容。到目前为止很棒。您需要解决的最后一个问题是资源。要处理这个问题,您需要添加
Themes\Generic.xaml
并将库中的所有资源移入其中。然后,库可以找到它需要的所有资源,并且应用程序可以根据需要覆盖它。这是一篇很好的文章,讨论了与跨多个程序集的资源相关的一些问题:
另外,请务必设置控件程序集属性,以便搜索
Generic.xaml
:编辑:
上述解决方案适用于自定义控件,不适用于到
UserControl
资源。要在主应用程序可以覆盖的单独程序集中处理UserControl
资源,您可以使用以下方法:{DynamicResource ...}
并简单地将资源定义排除在外用户控件
。这样做的问题是,虽然资源在运行时会被正确定位,但在设计时不会找到它,这会使您的
UserControl
非常难以使用。此问题有两种解决方法:UserControl
并将设计时资源包含在 XAML 本身中。对于 Visual Studio,您可以将其包含在
UserControl
中仅在设计时:完成设计后,只需在 XAML 中注释掉该代码即可。这很不方便,但在 Visual Studio 支持设计时资源之前,至少它可以工作。
Really what you are asking is what is the best approach to splitting up a WPF application into multiple assemblies. Your strategy is to create reusable control libraries and let the app integrate everything. Great so far. The last issue you need to address is resources. To handle that you need to add a
Themes\Generic.xaml
and move all the resources in your library into it. Then the library can find all the resources it needs and the application can override it if it so wishes.Here is a good article that discusses some of the issues related to resources across multiple assemblies:
Also be sure to set up your control assembly attributes so that
Generic.xaml
will be searched:Edit:
The above solution applies to custom controls, not to
UserControl
resources. To handleUserControl
resources in a separate assembly that the main application can override, you can use this approach:{DynamicResource ...}
and simply leave the resource definition out of theUserControl
.The problem with this is that although the resource will be located correctly at run-time, it won't be found during design-time and this can make your
UserControl
very hard to work with. There are two workarounds for this problem:UserControl
and include design time resources in the XAML itself.For Visual Studio you can include this in your
UserControl
while designing it only:Once you are done designing it, simply comment out that code in the XAML. It's inconvenient, but until Visual Studio supports design time resources, at least it works.