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 cl
To solve this problem I did (I really wanted to work at design time in VisualStudio 2010):
public string SourcePath { get; set; }
public new Uri Source
{
get
{
if (IsInDesignMode)
{
return base.Source;
}
else
{
return _sourceUri;
}
}
set
{
if (value == null)
return;
if (IsInDesignMode)
{
var dict = Application.LoadComponent(new Uri(SourcePath, UriKind.Relative)) as ResourceDictionary;
MergedDictionaries.Add(dict);
return;
}
_sourceUri = value;
if (!_sharedDictionaries.ContainsKey(value))
{
base.Source = value;
_sharedDictionaries.Add(value, this);
}
else
{
MergedDictionaries.Add(_sharedDictionaries[value]);
}
}
}
and in my XAML:
<SharedResourceDictionary SourcePath="JooThemes;component/Buttons/Small/SettingsToggleStyle.xaml" Source="/JooThemes;component/Buttons/Small/SettingsToggleStyle.xaml" />
I know this issue is old and solved, but as I have been working on an alternative solution with a friend, I wanted to share it:
1. Use the WPF ResourceDictionary everywhere in xaml, this way Blend and the VS designer does not break.
2. Reference nuget packages Sundew.Xaml.Optimizations and Sundew.Xaml.Optimizer
3. Add a sxo-settings.json in the root of your project and enable the ResourceDictionaryCachingOptimizer
4. Build
The build will use a caching/shared ResourceDictionary.
For more details see: https://github.com/hugener/Sundew.Xaml.Optimizations
And the sample:
https://github.com/hugener/Sundew.Xaml.Optimizer.Sample
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]);
}
}
}