Bog*_*nik 3 xaml resourcedictionary mergeddictionaries windows-runtime
我需要将应用程序样式分成几个xaml文件.但我还需要定义一些共享值
<x:Double x:Key="SharedValue">100</x:Double>
Run Code Online (Sandbox Code Playgroud)
在单个文件中,在其他文件中定义的样式中使用此值.例如:
<Style x:Name="SomeStyle" TargetType="TextBox">
<Setter Property="Width" Value="{StaticResource SharedValue}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
在另一个资源字典文件中:
<Style x:Name="AnotherStyle" TargetType="Button">
<Setter Property="Height" Value="{StaticResource SharedValue}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在App.xaml文件中定义合并资源字典时
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="DefinedValues.xaml"/>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
我得到这个运行时异常:"Message ="找不到名称/密钥SharedValue的资源"
你能告诉我这样做是否可行以及我做错了什么?谢谢.
如果您在其他合并字典之间存在依赖关系,则使用合并字典可能会有点棘手.
当您有多个应用程序范围资源时,声明的顺序很重要.它们按照声明的相反顺序解析,因此在您的情况下,您应该有订单.
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
<ResourceDictionary Source="DefinedValues.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
此外,您可能需要在Styles1.xaml中引用其他ResourceDictionary.这在Styles1.xaml中适用于我.
<ResourceDictionary "...">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SharedValues.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Name="AnotherStyle"
TargetType="Button">
<Setter Property="Height"
Value="{StaticResource SharedValue}" />
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)