我需要将应用程序样式分离到多个 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 =“无法找到具有名称/ key 共享值的资源”
你能告诉我是否可以这样做以及我做错了什么? 谢谢。
最佳答案
如果其他合并字典之间存在依赖关系,那么使用合并字典可能会有点棘手。
当您有多个应用程序范围的资源时,声明的顺序很重要。它们按照声明的相反顺序解析,因此在您的情况下,您应该有顺序。
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles1.xaml"/>
<ResourceDictionary Source="Styles2.xaml"/>
<ResourceDictionary Source="DefinedValues.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
此外,您可能需要引用 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>
关于xaml - winrt xaml合并资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13001421/