本文介绍了winrt xaml 合并资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将应用程序样式分离到多个 xaml 文件中.但我还需要定义一些共享值,例如

I need to separate application styles to several xaml files.But I also need to define some shared values like

<x:Double x:Key="SharedValue">100</x:Double>

在单个文件中用于在其他文件中定义的样式中使用此值.例如:

in single file for use this value in styles defined in other files.For instance:

<Style x:Name="SomeStyle" TargetType="TextBox">
     <Setter Property="Width" Value="{StaticResource SharedValue}"/>
</Style>

并在另一个资源字典文件中:

and in another resource dictionary file:

<Style x:Name="AnotherStyle" TargetType="Button">
     <Setter Property="Height" Value="{StaticResource SharedValue}"/>
</Style>

但是当我尝试在 App.xaml 文件中定义合并的资源字典时

But when I try to define merged resource dictionary in App.xaml file

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

I get this runtime exception:"Message = "Cannot find a Resource with the Name/Key SharedValue"

你能告诉我这样做是否可行以及我做错了什么?谢谢.

Can you tell me is it posible to do this and what I'm doing wrong?Thanks.

推荐答案

如果其他合并词典之间存在依赖关系,则使用合并词典可能会有些棘手.

Using merged dictionaries can get a bit tricky if you have dependencies between other merged dictionaries.

当您有多个应用程序范围的资源时,声明的顺序很重要.它们以与声明相反的顺序解析,因此在您的情况下,您应该有顺序.

When you have multiple application-scope resources the order of the declare is important. They are resolved in the inverse order of the declare, so in your case you should have the order.

<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 中对我有用.

Also, you may need to reference the other ResourceDictionary in Styles1.xaml. This worked for me in 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>

这篇关于winrt xaml 合并资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:33