【发布时间】:2012-10-21 19:33:48
【问题描述】:
我需要将应用程序样式分成几个 xaml 文件。 但我还需要定义一些共享值,例如
<x:Double x:Key="SharedValue">100</x:Double>
在单个文件中用于在其他文件中定义的样式中使用此值。 例如:
<Style x:Name="SomeStyle" TargetType="TextBox">
<Setter Property="Width" Value="{StaticResource SharedValue}"/>
</Style>
在另一个资源字典文件中:
<Style x:Name="AnotherStyle" TargetType="Button">
<Setter Property="Height" Value="{StaticResource SharedValue}"/>
</Style>
但是当我尝试在 App.xaml 文件中定义合并的资源字典时
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefinedValues.xaml"/>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
我得到这个运行时异常:"Message = "Cannot find a Resource with the Name/Key SharedValue"
你能告诉我这样做是否可行以及我做错了什么? 谢谢。
【问题讨论】:
-
您在哪里看到的消息?您是否尝试使用 {StaticResource} 访问 XAML 中的资源?
-
当它在 App.g.i.cs 文件中作为运行时未处理的异常处理时出现此错误,但智能不会将我的 SharedValue 点亮为无法在任何地方识别。
-
是的,这是可能的,我能够使用您的示例创建一个应用程序,它适用于我的项目。您确定您的 App.xaml 文件中有 DefinedValues.xaml 和其他两个 ResourceDictionaries 的正确路径吗?
-
路径是正确的,但我有几个我不知道的错误。第一个是我在声明字典的顺序上犯了错误,第二个是我没有在依赖字典中引用 SharedValues 字典。在我纠正这些错误后,一切正常。谢谢,你帮了我!
标签: xaml windows-runtime resourcedictionary mergeddictionaries