Windows phone 7 WP7芒果资源目录合并
我对WP7 Mango中的ResourceDictionary有问题 我在互联网上能找到的大部分内容都很简单: 1) 包含正文的Xaml文件:Windows phone 7 WP7芒果资源目录合并,windows-phone-7,resourcedictionary,mergeddictionaries,Windows Phone 7,Resourcedictionary,Mergeddictionaries,我对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="TextBlockSty
<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文件中添加资源
有人能在这里指出解决方案吗?您需要在App.xaml中为ResourceDictionary设置一个键
<Application.Resources>
<ResourceDictionary x:Key="keyname">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
事实上已经解决了 我试图在没有钥匙的情况下让它工作,结果发现我在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所建议的,您不应该将密钥放入ResourceDictionary中
2) 引用的Xaml中的样式都应包含键(或名称)
3) 其余的样式需要按照上面的解释放置
4) 在下面的代码中,如果您重新定义了一些其他样式所基于的基本样式,则只有在MyResources2.xaml中重新定义继承的样式后,更改才会反映出来(或者将MyResources.xaml中的基本样式替换为MyResources2.xaml中的样式)
5) MergedDictionaries中的ResourceDictionaries的功能与LIFO一样首先,确保文件“MyResources.xaml”存在,并且其“Build Action”设置为“Resource”。我认为构建操作不需要资源,因为正如我在问题文本中所写,我只是在页面构建操作上运行它,我尝试添加了一个不好的结果,我刚刚意识到你的源代码没有corect格式。它应该是
Source=“/{projectname};component/MyResource.xaml”
。我在项目中有更多的Xaml细节,只需像我那样调用它就足够了(我让它运行了,但由于某些原因,我的答案没有在这里发布)
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
<ResourceDictionary Source="MyResources2.xaml"/>
</ResourceDictionary.MergedDictionaries>