Windows应用商店应用-资源字典样式问题

Windows应用商店应用-资源字典样式问题,windows,microsoft-metro,resourcedictionary,Windows,Microsoft Metro,Resourcedictionary,在Windows应用商店应用程序中,我无法通过StaticResource绑定使用一个资源字典中的资源作为第二个资源字典样式的属性设置器的值 下面是我尝试做的一个例子: 字典1.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

在Windows应用商店应用程序中,我无法通过StaticResource绑定使用一个资源字典中的资源作为第二个资源字典样式的属性设置器的值

下面是我尝试做的一个例子:

字典1.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="SomeBrush" Color="Black" />
</ResourceDictionary>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="SomeStyle" TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource SomeBrush}" />
    </Style>
</ResourceDictionary>
<Application
    x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                <ResourceDictionary Source="Common/Dictionary1.xaml"/>
                <ResourceDictionary Source="Common/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

字典2.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="SomeBrush" Color="Black" />
</ResourceDictionary>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="SomeStyle" TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource SomeBrush}" />
    </Style>
</ResourceDictionary>
<Application
    x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                <ResourceDictionary Source="Common/Dictionary1.xaml"/>
                <ResourceDictionary Source="Common/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

App.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="SomeBrush" Color="Black" />
</ResourceDictionary>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="SomeStyle" TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource SomeBrush}" />
    </Style>
</ResourceDictionary>
<Application
    x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                <ResourceDictionary Source="Common/Dictionary1.xaml"/>
                <ResourceDictionary Source="Common/Dictionary2.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

不管我做什么,这都不行。应用程序将不会启动,而是抛出一个未处理的异常,其效果是“找不到键为'SomeBrush'的资源”

我尝试过在App.xaml中改变顺序,玩嵌套合并字典等等

通过这样做,我成功地使其工作,但这不是一个选项:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="SomeStyle" TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource SomeBrush}" />
    </Style>
</ResourceDictionary>

在运行时,将清除App.Resources.MergedDictionaries,并根据各种条件动态加载各种资源字典。Dictionary1.xaml和Dictionary2.xaml都是独立加载的,并且可能包含不同的资源,具体取决于这些条件,因此不能以这种方式合并它们。它们必须在设计时包含在App.xaml中,以支持。。。。设计

有人知道这里发生了什么吗?这是虫子吗


谢谢

我相信我理解这里发生的事情,我将尝试解释我的观点,希望它会有一些意义

从我所看到的情况来看,在玩游戏并能够复制您看到的问题之后,在我看来,使用
StaticResource
关键字意味着它所指的键需要在ResourceDictionary的“范围”中可用

这可以解释为什么您的第二次尝试有效,因为您已经将
Dictionary1.xaml
Dictionary2.xaml
合并,因此
SomeBrush
可以被视为“在范围内”,并且可以工作

显然,在第一个场景中,
Dictionary1.xaml
中定义的键被认为是
Dictionary2.xaml
的“范围之外”。但它将被视为申请的“范围”

我在很大程度上是基于我所观察到的,但我在MSDN页面上也发现了以下句子:

在ResourceDictionary的范围内,检查字典的键唯一性。但是,该范围不会扩展到合并字典中的不同项


我今天在一个通用应用程序项目中遇到了类似的问题。问题是,合并的字典需要一个带有
ms-appx
-协议的
源路径

<ResourceDictionary Source="ms-appx:///Common/StandardStyles.xaml"/>
...

...
它解决了你的问题吗