【问题标题】:Custom resource dictionary inside ControlTemplate or DataTemplateControlTemplate 或 DataTemplate 中的自定义资源字典
【发布时间】:2012-11-10 14:19:31
【问题描述】:

编辑:在使用标准 .NET ResourceDictionary 时也会出现此问题,并且似乎是在控件或数据模板中使用资源字典时出现的问题。

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

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 元素内引用它时,就会显示虚假错误(这些不影响构建,仍然成功)。

这个显示为标准 ResourceDictionary,它在其合并字典中包含 SharedResourceDictionary:

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>

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

谢谢

【问题讨论】:

  • 常用方法?您是否有任何链接指向有关此技术的更广泛讨论?
  • 这是一个……还有更多,我稍后会在有时间的时候挖掘。 softnotes.wordpress.com/2011/04/05/…
  • 我对桌面应用程序(.NET 3.5SP1 和 4.0)使用了类似的实现,它在运行时工作。为了避免 VS 设计器中的错误,我使用 #if !DEBUG 指令从 SharedResourceDictionary 类中排除代码。
  • 嗨“user1835941”:) 你排除了课程的全部内容吗?我认为我的 VisualStudio.IsInDesignMode 可能会做类似的事情,而且在调试时也能看到该类的好处。

标签: wpf xaml datatemplate controltemplate resourcedictionary


【解决方案1】:

此问题已报告给 Microsoft。您可以对其进行投票,所以它可能会在未来的某个版本中得到修复。

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

【讨论】:

    猜你喜欢
    • 2012-09-03
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多