如何引用在另一个库中的单独程序集中定义的 WPF 样式键
我有两个库和一个应用程序组装项目布局,我想知道如何通过其中一个库中的键引用另一个库中定义的样式。
我的解决方案布局:
- WpfControls.dll - 包含通用可重用控件,例如基于 WPF 的 NumericUpDown 控件以及其他基本控件和一些我希望可重用的样式。为了举例说明,假设我的样式键定义为 MyButtonStyle。
- SpecializedControls.dll - 保存专门用于我们的应用程序的控件,例如 WPF 中的自定义复合控件和 UserControls。这是我想引用 WpfControls.dll 程序集中定义的 MyButtonStyle 样式的地方。
- Application.exe - 此程序集使用 SpecializedControls.dll 程序集中定义的控件。
我已经看到一些示例解释了如何使用简单的单个控件库和应用程序程序集来执行此操作,但就我而言,我有两个程序集和一个应用程序。换句话说,在我的例子中,我的第二个控件库中没有 App.xaml,我可以在其中使用 MergeDictionaries 概念。我很确定如果我愿意,我可以直接在每个控件 XAML 文件中放置 MergedDictionaries 引用,以合并 WpfControls.dll 程序集中定义的所有 Generic.xaml,但我的猜测是,这会导致样式被冗余地合并到各个位置在我的 SpecializedControls 程序集中,这似乎不是最好的解决方案。更糟糕的是,这些字典也可能被合并到 Application.exe 中,这是否会在运行时使我的应用程序变得臃肿?
任何人都可以澄清以最少的程序和资源开销共享此类样式的推荐或最佳方法是什么。
更新:经过更多测试后,即使在同一个程序集中,我也无法通过其键引用 Generic.xaml 中定义的任何资源。我对资源键如何工作的理解可能完全错误,因此我必须进行更多研究并研究 ComponentResourceKey。如果有人有任何提示或建议,请帮忙。
I have two libraries and a single application assembly project layout and I'm wondering how can I reference styles by their key in one of my libraries that's defined in the other.
My solution layout:
- WpfControls.dll - Holds generic reusable controls such as a WPF based NumericUpDown control as well as other basic controls and some styles as well that I would like to make reusable. For the sake of the example lets say my style key here is defined as MyButtonStyle.
- SpecializedControls.dll - Holds controls that are specialized to our application such as custom composite controls and UserControls in WPF. This is where I'd like to reference the MyButtonStyle style defined in WpfControls.dll assembly.
- Application.exe - This assembly uses controls defined in the SpecializedControls.dll assembly.
I've seen some examples explaining how to do this with a simple single control library and application assembly but in my case I have two assemblies and an application. So in other words in my case I don't have an App.xaml in my second control library where I could use the MergeDictionaries concept. I'm pretty sure if I wanted to I could put a MergedDictionaries reference directly in each controls XAML file to merge all of Generic.xaml defined in WpfControls.dll assembly but my guess is that this would result in styles being redundantly merged into various places in my SpecializedControls assembly which doesn't seem like the best solution. To make matters worse, these dictionaries might also be merged into Application.exe as well, does this begin to bloat my application at runtime?
Can anybody clarify what the recommended or best approach for sharing such styles with the least amount of programtic and resource overhead.
Update: After a bit more testing it appears that I'm having trouble referencing any resource defined within Generic.xaml by it's key even within the same assembly. There may be something totally wrong about how I'm understanding how resource keys work so I'll have to a bit more research as well as looking into ComponentResourceKey. If anybody has any hints or suggestions please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WPF 中的资源查找按层次结构进行:沿着逻辑树向上,然后是应用程序资源、主题资源,最后是系统资源。
主题资源通常只能隐式访问(即使在定义它们的程序集中也是如此)。这仅与样式相关,其中 TargetType 可以用作隐式键。
要完成您想要做的事情,有两个选项:
使用ComponentResourceKey。这是一个特殊的资源键,允许从其他程序集引用资源。例如,您可以在WpfControls主题字典中定义画笔:
然后您可以在 SpecializedControls 中引用它:
使用MergedDictionaries导入将字典放入应用程序资源中。您可以在Application程序集中执行此操作,并且当应用程序加载时,甚至SpecializedControls中的控件也可以使用这些资源。在这种情况下,您将遇到设计时体验问题,您可以通过在 SpecializedControls 中放置一个假的 App.xaml 来解决这个问题,该文件还包含对字典的引用。
希望这会有所帮助,
Aelij。
Resource lookup in WPF works in a hierarchy: up the logical tree, then application resources, theme resources and finally system resources.
Theme resources can usually be accessed only implicitly (even within the assembly they are defined). This is relevant only for Styles, where the TargetType can be used as the implicit key.
To accomplish what you're trying to do, there are two options:
Use a ComponentResourceKey. This is a special resource key which allows referencing resources from other assemblies. For example, you can define a brush in WpfControls theme dictionary:
And then you can reference it in SpecializedControls:
Use MergedDictionaries to import a dictionary into the application resources. You can do this in the Application assembly, and when the application loads, even controls that are in SpecializedControls would be able to use these resources. You will have a problem with the design-time experience in this scenario, which you can solve by putting a fake App.xaml in SpecializedControls, that would also contain a reference to the dictionary.
Hope this helps,
Aelij.
接受的答案是正确的,我只是解释如何
在App.xaml中使用MergedDictionaries,
The accepted answer is correct, I'm Just explaining how to use MergedDictionaries
In App.xaml,