I have an assembly called Atelis.Base.Wpf.Resources. It is a DLL that holds resource dictionaries.
Within this assembly I have defined a SharedResourceDictionary:
namespace Atelis.Base.Wpf.Resources
{
/// <summary>
/// The shared resource dictionary is a specialized resource dictionary
/// that loads it content only once. If a second instance with the same source
/// is created, it only merges the resources from the cache.
/// </summary>
public class SharedResourceDictionary : ResourceDictionary
{
/// <summary>
/// Internal cache of loaded dictionaries
/// </summary>
public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
new Dictionary<Uri, ResourceDictionary>();
/// <summary>
/// Local member of the source uri
/// </summary>
private Uri _sourceUri;
/// <summary>
/// Gets or sets the uniform resource identifier (URI) to load resources from.
/// </summary>
public new Uri Source
{
get { return _sourceUri; }
set
{
try
{
_sourceUri = new Uri(value.OriginalString);
}
catch
{
// do nothing?
}
if (!_sharedDictionaries.ContainsKey(value))
{
// If the dictionary is not yet loaded, load it by setting
// the source of the base class
base.Source = value;
// 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]);
}
}
}
}
}
I then have a number of resource dictionary files like so:
Styles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- styles are defined here -->
</ResourceDictionary>
Colors.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Color x:Key="Color1">#FF000000</Color>
<!-- ... many more colors and brushes are defined here -->
</ResourceDictionary>
Buttons.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Atelis.Base.Wpf.Resources"
>
<ResourceDictionary.MergedDictionaries>
<local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Colors.xaml"/>
<local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- a ton of button styles are defined here -->
</ResourceDictionary>
DataTemplates.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:base="clr-namespace:Atelis.Base.Client;assembly=Atelis.Base.Wpf"
xmlns:local="clr-namespace:Atelis.Base.Wpf.Resources"
>
<ResourceDictionary.MergedDictionaries>
<local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Buttons.xaml"/>
<local:SharedResourceDictionary Source="pack://application:,,,/Atelis.Base.Wpf.Resources;component/Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- a ton of templates are defined here -->
</ResourceDictionary>
Everything is good until I get to DataTemplates.xaml. The Buttons.xaml SharedResourceDictionary is giving me an inline warning "Cannot create unknown type '{clr-namespace:Atelis.Base.Wpf.Resources}SharedResourceDictionary'." And when I try to define a staticresource style I get another xaml warning "The resource 'ButtonStyle2' cannot be resolved."
I think it is important to notice that the other SharedResourceDictionary declarations whose source URI do not contain SharedResourceDictionaries do not have this problem (I can add Colors and Styles to dictionary declarations without any warnings and the resources resolve when set).
The application works just fine, but I cannot seem to get visual studio to resolve this resource and remove the warning. It is a small inconvenience but I am curious to know why it is happening.