问题描述
我使用自定义类在我的 WPF 应用程序中实现共享资源功能,这是创建和管理字典的示例代码
I have used a custom class to implement shared resource functionality in my WPF application this is a sample code to create and manage dictionaries
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> SharedDictinaries = new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
private static bool IsInDesignMode
{
get
{
return (bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
typeof(DependencyObject)).Metadata.DefaultValue;
}
}
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get
{
if (IsInDesignMode)
{
return base.Source;
}
return _sourceUri;
}
set
{
if (!IsInDesignMode)
{
base.Source = value;
return;
}
_sourceUri = value;
if (!SharedDictinaries.ContainsKey(value))
{
base.Source = value;
SharedDictinaries.Add(value, this);
}
else
{
MergedDictionaries.Add(SharedDictinaries[value]);
}
}
}
}
这个文件是在一个单独的程序集中实现的,我已经在我的 shell WPF 应用程序中引用了它.
this File is implemented i a separate assembly and I have refferenced it in my shell WPF application.
我在 app.xaml 中以下列方式定义了我的资源
I have my resources defined in app.xaml int the following way
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<Infrastructure:SharedResourceDictionary Source="pack://application:,,,/CuratioCMS.Client.Resources;Component/Themes/General/Brushes.xaml" />
<Infrastructure:SharedResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Office2010/Silver.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
如果我删除 Brushes.xaml 它可以工作但是当我切换到设计视图时使用此字典我会收到以下错误
if I remove Brushes.xaml it works but with this dictinary in place as I switch to design view I get following error
调用的目标已抛出异常
有人可以帮我找出问题所在吗?
could someone help my to figure out the problem?
推荐答案
我在某处读到,这是一个内存问题@design-time.我在 Source of Source 中解决了它:
I read somewhere, that it is a memory issue @design-time. I solved it in the Setter of Source:
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get { return _sourceUri; }
set
{
_sourceUri = value;
if (!_sharedDictionaries.ContainsKey(value))
{
try
{
//If the dictionary is not yet loaded, load it by setting
//the source of the base class
base.Source = value;
}
catch (Exception exp)
{
//only throw exception @runtime to avoid "Exception has been
//thrown by the target of an invocation."-Error@DesignTime
if( ! IsInDesignMode )
throw;
}
// 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]);
}
}
}
这篇关于WPF 共享资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!