编辑:使用标准.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>
有没有人有任何想法如何消除这种滋扰错误?
由于
答案 0 :(得分:0)
此问题已报告给Microsoft。您可以对其进行投票,因此可能会在将来的某个版本中修复。