When you’re writing XAML using the Visual Studio designer, you’ll sometimes find that it’s unable to locate resources that are stored in a ResourceDictionary (and referenced using
This is a particular problem if you’re working on a class library project (rather than a WPF application), as it often won’t be able to find the correct
Application to start, and so won’t load its resources.
It can also happen if you’re loading ResourceDictionaries dynamically.
The solution is badly-documented, but seems to work fairly well.
First, create a new ResourceDictionary which contains the XAML files which VS is having trouble displaying at design time.
I usually call it
DesignTimeResources.xaml, and put it in
I’ve found you need to add it to both the library containing the XAML files, and the exe which references it.
This ResourceDictionary will just reference the ResourceDictionaries which you want Visual Studio to load at design time.
I normally find it easiest if I structure my ResourceDictionaries so that there is one “root” ResourceDictionary which references all of the others, and then I reference that root ResourceDictionary from both my application’s resources and from
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyAssembly;component/Presentation/Common/Resources/Main.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
Then edit your
csproj, and add the following
<ItemGroup> <Page Include="Properties\DesignTimeResources.xaml"> <SubType>Designer</SubType> <Generator>MSBuild:Compile</Generator> <ContainsDesignTimeResources>true</ContainsDesignTimeResources> </Page> </ItemGroup>
Reload the project, and the XAML designer, and everything may now work…
I’ve had Expression Blend offer to create
DesignTimeResources.xaml in the past, which is how I learnt this trick, but I can’t remember what prompted it do this.