Custom resource dictionary inside ControlTemplate or DataTemplate

强颜欢笑 提交于 2019-12-10 13:31:20

问题


EDIT: This problem occurs when using the standard .NET ResourceDictionary as well and appears to be an issue with using resource dictionaries inside control or data templates.

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);
        }
    }
}

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

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'

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

Thanks


回答1:


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



来源:https://stackoverflow.com/questions/13508385/custom-resource-dictionary-inside-controltemplate-or-datatemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!