1

我有一个名为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>

颜色.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>

按钮.xml

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

数据模板.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' 无法解析。”

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

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

4

1 回答 1

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>
于 2015-01-30T04:21:26.823 回答