winrt xaml合并资源

winrt xaml合并资源,xaml,windows-runtime,resourcedictionary,mergeddictionaries,Xaml,Windows Runtime,Resourcedictionary,Mergeddictionaries,我需要将应用程序样式分离到几个xaml文件中。 但我也需要定义一些共同的价值观,比如 <x:Double x:Key="SharedValue">100</x:Double> 100 在单个文件中,用于在其他文件中定义的样式中使用此值。 例如: <Style x:Name="SomeStyle" TargetType="TextBox"> <Setter Property="Width" Value="{StaticResource Sha

我需要将应用程序样式分离到几个xaml文件中。 但我也需要定义一些共同的价值观,比如

<x:Double x:Key="SharedValue">100</x:Double>
100
在单个文件中,用于在其他文件中定义的样式中使用此值。 例如:

<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 SharedValue的资源”

你能告诉我这有可能吗?我做错了什么?
谢谢。

如果您在其他合并词典之间存在依赖关系,那么使用合并词典可能会有点棘手

当您有多个应用程序作用域资源时,声明的顺序很重要。它们是按声明的相反顺序解析的,因此在您的情况下,您应该有顺序

<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>


您在哪里看到消息?您是否试图使用{StaticResource}访问XAML中的资源?当在App.g.I.cs文件中作为未处理的异常在运行时处理时,我会遇到此错误,但Intellience不会将我的SharedValue视为任何地方都无法识别。是的,这是可能的,我可以使用您的示例创建一个应用程序,它可以在我的项目中工作。您确定您有到DefinedValues.xaml和其他两个的正确路径吗你的App.xaml文件中的sourceDictionaries?路径是wright,但我有几个不知道的错误。第一个错误是我在声明的字典的顺序上出错,第二个错误是我没有在从属字典中引用SharedValue字典。在我更正这些错误后,一切正常。谢谢,你帮助了我!谢谢,this正是我想要的!仅供参考,当你得到一个可接受的答案时,你可以投票表决,但你也应该“接受””他回答。这样,你在StackOverflow上的声誉就会提高,人们在将来更可能帮助你。第二部分是重要的一部分。在我的例子中,文件的顺序并不重要,但是在我的模板中注册样式确实有帮助。谢谢