【问题标题】:Trouble creating nested merged dictionaries using SharedResourceDictionary使用 SharedResourceDictionary 创建嵌套合并字典时遇到问题
【发布时间】:2014-12-29 17:10:55
【问题描述】:

我有一个名为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]);
            }
        }
    }
}
}

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

样式.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 给我一个内联警告“无法创建未知类型'{clr-namespace:Atelis.Base.Wpf.Resources}SharedResourceDictionary'。”当我尝试定义 staticresource 样式时,我收到另一个 xaml 警告 “资源 'ButtonStyle2' 无法解析。”

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

该应用程序运行良好,但我似乎无法让 Visual Studio 解决此资源并删除警告。这是一个小小的不便,但我很想知道为什么会这样。

【问题讨论】:

    标签: wpf xaml resourcedictionary mergeddictionaries


    【解决方案1】:

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

    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>
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2014-08-31
      • 2016-02-09
      • 2010-11-16
      • 1970-01-01
      • 2019-05-29
      • 2019-03-26
      • 1970-01-01
      相关资源
      最近更新 更多