Search code examples
c#wpfxamlresourcedictionarydesign-time

How can I get my program to pick up application-scoped resources defined in my Application.Resources during design time?


Application-scoped resources are not being picked up during design time.

I have my App.xaml (renamed 'Program.xaml') populated with a reference to my resource dictionary -

<Application
    x:Class="OffendingProgram.Program"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:PersonAI"
    StartupUri="AIAdventure.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Controls/OffendingDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

I have defined my resource dictionary within a 'Controls' folder with the following -

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ControlTemplate
        x:Key="OffendingTemplate"
        TargetType="{x:Type RepeatButton}" />
    <Style
        x:Key="{x:Type RepeatButton}"
        TargetType="{x:Type RepeatButton}">
        <Setter Property="Template" Value="{StaticResource OffendingTemplate}" />
    </Style>
</ResourceDictionary>

I have my Main Window set up as follows -

<Window x:Class="OffendingProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OffendingProgram"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Background="Black">
    <RepeatButton />
</Window>

During design-time, I can see my 'RepeatButton' occupying the full window (I set the window background to black just to be sure). Yet during run-time, the button vanishes (as expected, since it's picking up an implicit style setting the control template to an empty template).

This is just a Minimal, Complete and Verifiable example meant to demonstrate my issue - that the resources are not being applied during design time.

What am I missing to make my resources get picked up during design-time?


Solution

  • I found the answer. Though the approach may vary based on the version of Visual Studios one is using.

    1. Edit your Project File

    2. For VS2022, Add the following -

       <ItemGroup>
           <None Include="Properties\DesignTimeResources.xaml">
               <SubType>Designer</SubType>
               <Generator>MSBuild:Compile</Generator>
               <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
           </None>
       </ItemGroup>
      

    where Include="..." paths to your DesignTimeResources.xaml file.

    1. Reload your project.

    Any resource dictionary included within the MergedDictionaries property of the ResourceDictionary defined therein will be picked up and utilized during Design Time.