尝试使用 ResourceDictionary,但其中的样式未找到
我有一个 Silverlight 类库,名为 MyClassLibrary。
其中,我有一个名为 MyControl 的用户控件。
在控件中,我定义了用户资源:
<UserControl.Resources>
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
(lots of xaml)
</Style>
</UserControl.Resources>
控件使用这样的样式:
<ComboBox Style="{ StaticResource ComboBoxStyle }"></ComboBox>
这一切都很完美,组合框提供了正确的样式,所以我知道样式编写正确。
我真正想要的是让样式位于资源字典中,以便该程序集中的多个不同控件可以使用它。因此,我在同一个程序集中创建一个资源字典。我将其称为 ResourceDictionary.xaml。
我将样式定义从用户控件移至资源字典。
那么资源字典看起来像这样:
<ResourceDictionary
xmlns="etc" >
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
(lots of xaml)
</Style>
</ResourceDictionary>
控件的用户资源现在看起来像这样:
<UserControl.Resources>
<ResourceDictionary
Source="/MyClassLibrary;component/ResourceDictionary.xaml" x:Name="resDict"/>
</UserControl.Resources>
并且控件仍然以与以前完全相同的方式使用样式。
现在我知道它正在查找 ResourceDictionary.xaml 文件,因为我尝试将“Source”属性更改为 NonExistentFile.xaml,但它抱怨找不到该文件。它没有对 ResourceDictionary.xaml 提出投诉,所以我认为它正在找到它。
但是,当我运行该应用程序时,出现错误“找不到具有名称/键 ComboBoxStyle 的资源”。
我做错了什么?这看起来很简单,但行不通。
预先感谢您能给我的任何帮助。
I have a Silverlight class library, called MyClassLibrary.
In it, I have a user control, called MyControl.
Within the control I define user resources:
<UserControl.Resources>
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
(lots of xaml)
</Style>
</UserControl.Resources>
The control consumes the style like this:
<ComboBox Style="{ StaticResource ComboBoxStyle }"></ComboBox>
This all works perfectly, the ComboBox comes up with the right style, so I know the style is written correctly.
What I really want is to have the style be in a resource dictionary, so that it can be used by several different controls within this assembly. So I create, in the SAME assembly, a resource dictionary. I call it ResourceDictionary.xaml.
I move the Style definition from my user control to the resource dictionary.
So then the resource dictionary looks like this:
<ResourceDictionary
xmlns="etc" >
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
(lots of xaml)
</Style>
</ResourceDictionary>
The control's user resources now look like this:
<UserControl.Resources>
<ResourceDictionary
Source="/MyClassLibrary;component/ResourceDictionary.xaml" x:Name="resDict"/>
</UserControl.Resources>
And the control still consumes the style exactly the same way as before.
Now I know that it is finding the ResourceDictionary.xaml file, because I tried changing the "Source" attribute to NonExistentFile.xaml, and it complained that it couldn't find the file. It doesn't make that complaint with ResourceDictionary.xaml, so I presume it's finding it.
However, when I run the app, I get an error that "Cannot find a Resource with the Name/Key ComboBoxStyle".
What am I doing wrong? This seems so simple and it's not working.
Thanks in advance for any help you can give me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这是否有帮助,但我将我的 ResourceDictionaries 包含在 App.xaml 中:
即使您不喜欢这种方法,您也可以看到我的 Source= 与您的不同。
Not sure if this helps exactly but I include my ResourceDictionaries in App.xaml:
Even if you don't like the approach, you can see that my Source= is different from yours.
谢谢你们两位,你们的回答确实让我解决了这个问题。
我真正拥有的是这样的:
然后我添加了我的,所以它看起来像这样
现在这个编译得非常漂亮,但就是无法运行。我不明白我需要使用 MergedDictionaries。然后我得到了这个概念并重新组织了该部分,现在一切都很顺利。
非常感谢!
Thank you to both of you, your answers did actually allow me to solve it.
What I really had was something like this:
To which I then added my so it looked like this
Now this compiled very beatifully but just wouldn't run. I didn't understand that I needed to use MergedDictionaries. So then I got that concept and reorganized the section and now it all works beautifully.
Thanks very much!