使用SharedResourceDictionary创建嵌套的合并字典时遇到问题

时间:2014-12-29 17:10:55

标签: wpf xaml resourcedictionary mergeddictionaries

我有一个名为Atelis.Base.Wpf.Resources的程序集。它是一个包含资源字典的DLL。

在这个程序集中,我定义了一个SharedResourceDictionary

namespace Atelis.Base.Wpf.Resources
{

/// <summary>
/// The shared resource dictionary is a specialized resource dictionary
/// that loads it content only once. If a second instance with the same source
/// is created, it only merges the resources from the cache.
/// </summary>
public class SharedResourceDictionary : ResourceDictionary
{
    /// <summary>
    /// Internal cache of loaded dictionaries 
    /// </summary>
    public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
        new Dictionary<Uri, ResourceDictionary>();

    /// <summary>
    /// Local member of the source uri
    /// </summary>
    private Uri _sourceUri;

    /// <summary>
    /// Gets or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    public new Uri Source
    {
        get { return _sourceUri; }
        set
        {
            try
            {
                _sourceUri = new Uri(value.OriginalString);
            }
            catch
            {
                // do nothing?
            }

            if (!_sharedDictionaries.ContainsKey(value))
            {
                // If the dictionary is not yet loaded, load it by setting
                // the source of the base class
                base.Source = value;

                // add it to the cache
                _sharedDictionaries.Add(value, this);
            }
            else
            {
                // If the dictionary is already loaded, get it from the cache
                MergedDictionaries.Add(_sharedDictionaries[value]);
            }
        }
    }
}
}

然后我有许多资源字典文件,如下所示:

Styles.xaml

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

<!-- styles are defined here -->
</ResourceDictionary>

Colors.xaml

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

    <Color x:Key="Color1">#FF000000</Color>
    <!-- ... many more colors and brushes are defined here -->
</ResourceDictionary>

Buttons.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Atelis.Base.Wpf.Resources"
>
<ResourceDictionary.MergedDictionaries>
    <local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Colors.xaml"/>
    <local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Styles.xaml"/>           
</ResourceDictionary.MergedDictionaries>

<!-- a ton of button styles are defined here -->

</ResourceDictionary>

DataTemplates.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:base="clr-namespace:Atelis.Base.Client;assembly=Atelis.Base.Wpf"
                xmlns:local="clr-namespace:Atelis.Base.Wpf.Resources"
                >

<ResourceDictionary.MergedDictionaries>
    <local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Buttons.xaml"/>
    <local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>

<!-- a ton of templates are defined here -->

</ResourceDictionary>

在我到达DataTemplates.xaml之前,一切都很好。 Buttons.xaml SharedResourceDictionary给我一个内联警告 &#34;无法创建未知类型&#39; {clr-namespace:Atelis.Base.Wpf.Resources} SharedResourceDictionary&# 39;。&#34; 当我尝试定义staticresource样式时,我会收到另一个xaml警告 &#34;资源&#39; ButtonStyle2& #39;无法解决。&#34;

我认为重要的是要注意其源{URI不包含SharedResourceDictionary的其他SharedResourceDictionaries声明没有这个问题(我可以在没有任何警告的情况下将颜色和样式添加到字典声明中资源解决时设置)。

该应用程序工作得很好,但我似乎无法让visual studio解析此资源并删除警告。这是一个小小的不便,但我很想知道它为什么会发生。

1 个答案:

答案 0 :(得分:0)

我不确定你所做的事情是否可行。

AFAIK,这里有两种不同的方法

<local:Foo x:Key="Bar" />

<ResourceDictionary Source="Baz" />

但你的是两者的混合。

诀窍是,如果您收到 “资源'ButtonStyle2'无法解析。” ,请确保您没有任何其他错误并尝试构建你的项目。大多数时候我的声明是有效的,但编译器会抱怨它,直到我在XAML中构建它。

你也可以合并上面提到的这两种类型,如下所示。

<ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Baz" />
  </ResourceDictionary.MergedDictionaries>
  <local:Foo x:Key="Bar" />
</ResourceDictionary>