我在WP7芒果中遇到了ResourceDictionary的问题。
我在互联网上能找到的大多数东西都是这样简单的:
1)带body的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行时,它会运行,但崩溃时出现未指定的error.It,看起来它没有添加来自MyResources.xaml文件的资源。
有人能在这里指出一个解决方案吗?
发布于 2012-04-03 18:28:12
您需要在App.xaml中为ResourceDictionary设置一个关键点。
<Application.Resources>
<ResourceDictionary x:Key="keyname">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>发布于 2012-04-04 04:54:05
真的弄明白了。
我试图让它在没有钥匙的情况下工作,结果发现我留在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中的样式都应包含键(或名称)
3)其余的样式需要按照上面的说明放置
4)在下面的代码中,如果您重新定义了一些其他样式所基于的基本样式,更改将不会反映出来,直到您在MyResources2.xaml中也重新定义了继承的样式(或者将MyResources.xaml中的基本样式替换为MyResources2.xaml中的样式)
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
<ResourceDictionary Source="MyResources2.xaml"/>
</ResourceDictionary.MergedDictionaries> 5) MergedDictionaries中的ResourceDictionaries作为后进先出函数
https://stackoverflow.com/questions/9988828
复制相似问题