Search code examples
c#.netwpfxaml

VS2022: An Error Occurred While Finding the Resource Dictionary


I have read through all the similar questions to see if one of the solutions works for me, and so far none have worked for me. I am attempting to build a window project in C# WPF .NET, and I really want to start building a style system so that my next few projects will have the same look without me having to do too much modification to the styles I create. But I'm getting stuck after just creating a style for just one element (buttons).

Here is my Solution Explorer:

Solution Explorer

And this is how I'm including the Resource Dictionary:

<Window.Resources>
    <CollectionViewSource x:Key="AddinCollectionViewSource" CollectionViewType="ListCollectionView"/>
    <ResourceDictionary x:Key="RevitStyles">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/RevitAddinManager;component/Assets/RevitStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

I have tried a few other methods, including:

<ResourceDictionary x:Key="RevitStyles" Source="pack://application:,,,/RevitAddinManager;component/Assets/RevitStyles.xaml"/>

Source="/Assets/RevitStyles.xaml"

Source=/RevitAddinManager/Assets/RevitStyles.xaml"

And I'm sure there's a few other things I've tried that didn't work. I've also tried starting a brand new project to no avail, and excluding/including the problem file from/to the solution.

So my question is what could I be doing wrong? Could it be an issue with how I've written the ResourceDictionary? Or is there something else I'm missing that anyone can think of?

For reference, here is what I have in my dictionary so far:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="RevitButton" TargetType="{x:Type Button}">
        <Style.Resources>
            <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
            <SolidColorBrush x:Key="Button.Static.Border" Color="#FFA0A0A0"/>
            <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFE5F1FB"/>
            <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF0078D6"/>
            <SolidColorBrush x:Key="Button.Focused.Background" Color="{Binding Color, Source={StaticResource Button.Static.Background}}"/>
            <SolidColorBrush x:Key="Button.Focused.Border" Color="{Binding Color, Source={DynamicResource Button.MouseOver.Border}}"/>
            <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFCCE4F7"/>
            <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF005499"/>
            <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFC9C9C9"/>
            <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFA0A0A0"/>
            <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="DimGray"/>
        </Style.Resources>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
        <Setter Property="Foreground" Value="{Binding Forground}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            BorderBrush="{TemplateBinding BorderBrush}">
                        <ContentPresenter Name="Content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{StaticResource Button.MouseOver.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.MouseOver.Border}"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Background" Value="{StaticResource Button.Focused.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.Focused.Border}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{StaticResource Button.Pressed.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.Pressed.Border}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="{StaticResource Button.Disabled.Background}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource Button.Disabled.Border}"/>
                            <Setter Property="TextElement.Foreground" TargetName="Content" Value="{StaticResource Button.Disabled.Foreground}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

And the error I'm getting: An error occurred while finding the resource dictionary "<whatever path I've tried to provide>".


Solution

  • The solution Tarazed posted didn't work for me initially, but it looks like I managed to rename the file incorrectly (I did not name it on creation through Visual Studio's file creator), and that was preventing the solution from finding the file from the correctly typed out Source. I had previous tried recreating the file to no avail and it is likely what Tarazed posted was the other half of the answer I needed at the time to get it working. After implementing his changes and then recreating the file with the desired name, it works!