我在WP7 Mango中遇到ResourceDictionary问题。
我在网上找到的大部分内容都很简单:
1)带正文的Xaml文件:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextBlockStyle1" TargetType="TextBlock">
<Setter Property="Foreground" Value="Orange"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
</ResourceDictionary>
2)添加到App.xaml以下:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
不确定为什么它不起作用。当这样做时,我得到例外:
“ResourceDictionary”类型位于ResourceDictionary中,没有密钥。
当我在步骤2中将ked添加到第二个xaml行时,它会运行,但崩溃时会出现未指定的错误。看起来它不会从MyResources.xaml文件中添加资源。
有人可以在这里指出解决方案吗?
答案 0 :(得分:1)
您需要在App.xaml中为ResourceDictionary设置密钥。
<Application.Resources>
<ResourceDictionary x:Key="keyname">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
答案 1 :(得分:1)
实际上想通了。
我试图让它在没有密钥的情况下工作,并发现我在App.xaml中留下的样式正在解决问题。因此,App.xaml中剩下的所有剩余样式都必须移动到内部,即使它们是唯一的。
<Application.Resources>
<ResourceDictionary>
my remaining styles with key & target type are here now
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
编辑:
几个更重要的细节可能会节省一些人的时间,并花了很长时间才弄明白: 1)正如MSDN建议的那样,你不应该把Key放在ResourceDictionary中
2)引用的Xaml中的样式都应包含Key(或Name)
3)其他样式需要按照上面的说明放置
4)在下面的代码中,如果重新定义其他一些样式所基于的基本样式,则在MyResources2.xaml中重新定义继承样式之前不会反映更改(或者将样式替换为MyResources.xaml中的基本样式) MyResources2.xaml)
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
<ResourceDictionary Source="MyResources2.xaml"/>
</ResourceDictionary.MergedDictionaries>
5)MergedDictionaries中的ResourceDictionaries充当LIFO