本文介绍了ControlTemplate或DataTemplate中的自定义资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用标准的.NET ResourceDictionary时也会发生此问题,并且在控制或数据模板中使用资源字典时似乎会出现问题.

我有一个自定义资源字典,它遵循一种共享资源实例的通用方法.

I have a custom resource dictionary that follows a common approach to sharing resource instances.

http://softnotes.wordpress.com/2011/04/05/shared-resourcedictionary-for-silverlight/ http://www.wpftutorial.net/MergedDictionaryPerformance.html

public class SharedResourceDictionary : ResourceDictionary
{
    static readonly Dictionary<Uri, WeakReference<ResourceDictionary>> SharedDictionaries = new Dictionary<Uri, WeakReference<ResourceDictionary>>();

    Uri _sourceUri;

    public new Uri Source
    {
        get
        {
            // Behave like standard resource dictionary for IDE...
            if (VisualStudio.IsInDesignMode)
                return base.Source;

            return this._sourceUri;
        }
        set
        {
            // Behave like standard resource dictionary for IDE...
            if (VisualStudio.IsInDesignMode)
            {
                base.Source = value;
                return;
            }

            this._sourceUri = value;

            WeakReference<ResourceDictionary> cached;
            if (SharedDictionaries.TryGetValue(value, out cached))
            {
                ResourceDictionary rd;
                if (cached.TryGetTarget(out rd))
                {
                    this.MergedDictionaries.Add(rd);
                    return;
                }
            }

            base.Source = value;
            SharedDictionaries[value] = new WeakReference<ResourceDictionary>(this);
        }
    }
}

它工作正常,但是只要在ControlTemplate或DataTemplate中的Resources元素内引用它,就会显示虚假错误(这些错误不会影响生成,但仍会成功).

It works fine, but whenever it is referenced inside a Resources element within a ControlTemplate or DataTemplate, there are spurious errors shown (these do not affect the build, which still succeeds).

此示例显示为标准ResourceDictionary,该标准ResourceDictionary在合并的词典中包含SharedResourceDictionary:

This one gets shown for the standard ResourceDictionary which contains SharedResourceDictionary in its merged dictionaries:

Unable to cast object of type 'Microsoft.Expression.Markup.DocumentModel.DocumentCompositeNode' to type 'System.Windows.ResourceDictionary'

示例XAML:

<DataTemplate DataType="{x:Type vm:MyViewModel}">
    <DockPanel Style="{DynamicResource MainDockPanel}">
        <DockPanel.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <p:SharedResourceDictionary Source="/MyAssembly;component/MyResources.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </DockPanel.Resources>
    </DockPanel>
</DataTemplate>

有人知道如何消除这种讨厌的错误吗?

Does anyone have any ideas how I can eliminate this nuisance error?

谢谢

推荐答案

已经向Microsoft报告了此问题.您可以对其进行投票,所以也许它将在将来的某些版本中得到解决.

This issue has been reported to Microsoft. You can vote on it, so maybe it will get fixed in some future release.

https://connect.microsoft.com/VisualStudio/feedback/details/772730/xaml-designer-broken-when-adding-resource-dictionaries-to-data-or-control-templates

这篇关于ControlTemplate或DataTemplate中的自定义资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:08