2013-08-21 72 views
0

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

这里是什么,我试图做一个例子:

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

Dictionary2.xaml

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

的App.xaml

<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中以支持....设计。

有没有人知道这里发生了什么?这是一个错误?

谢谢!

回答

0

我相信我明白这里发生了什么,我会试着解释我的观点,希望它会有所帮助。

从我所看到的,在玩过并能够复制您所看到的问题后,它在我看来好像使用StaticResource关键字意味着它所指的密钥需要在“范围”中可用,的ResourceDictionary。

这可以解释为什么你的第二次尝试的工作,因为你已经合并Dictionary1.xamlDictionary2.xaml,因此SomeBrush可能“范围内”被认为是和它的工作。

很明显,在第一种情况下,Dictionary1.xaml中定义的密钥被认为是“超出范围”Dictionary2.xaml。但它会被认为是“范围内”的应用程序。

我立足于我已经观察到了很多,但我也发现在ResourceDictionary and XAML resource references页面上MSDN下面的句子:

内的ResourceDictionary的范围内,词典都检查关键唯一性。但是,该范围不会扩展到MergedDictionaries中的不同项目。

0

我今天有一个通用应用程序项目的类似问题。问题是,合并的字典需要Source-与ms-appx-协议的路径。

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

它解决了您的问题了吗?