xaml - winrt xaml合并资源

标签 xaml windows-runtime resourcedictionary mergeddictionaries

我需要将应用程序样式分离到多个 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/

相关文章:

c# - 使用 Targus Laptop Dock 的 Visual Studio Update 3 中的 System.Runtime.Remoting.RemotingException

c# - 是否可以为不同的 Windows 10 版本创建 UWP 应用程序?

c# - "Windows.System.Launcher"不启动写字板

wpf - ControlTemplate 或 DataTemplate 中的自定义资源字典

silverlight - Silverlight 3 中合并资源字典的正确使用

c# - 在 WPF 中,我可以在 2 个按钮之间共享相同的图像资源吗

c# - 在 C# 中将 RadioButton 绑定(bind)到 int

c# - 如何在 XAML 中将多个 StatusBarItems 对齐到右侧?

c# - 在Windows Phone运行时应用程序中使用Xna Framework类

c# - 获取 VariableSizedWrapGrid 进行虚拟化