Home > Software engineering >  Using ResourceDictionary results in Resource 'xxx' not found
Using ResourceDictionary results in Resource 'xxx' not found

Time:10-04

I'm currently trying to organize my UI Xaml by splitting it into multiple files whenever it makes sense.

Because of that I'm exporting my TitleBar Styles into an extra file (\UI\Styles\TitleBarButtonStyles.xaml):

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

<Style x:Key="TitleBarButton" TargetType="{x:Type Button}">
    <Setter Property="BorderThickness" Value="0,0,0,0" />
    <Setter Property="Height" Value="18" />
    <Setter Property="Width" Value="18" />
    <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" CornerRadius="10">
                    <TextBlock Text="{TemplateBinding Content}" FontFamily="Segoe MDL2 Assets" FontSize="10" HorizontalAlignment="Center" VerticalAlignment="Center" RenderOptions.ClearTypeHint="Auto" TextOptions.TextRenderingMode="Aliased" TextOptions.TextFormattingMode="Display" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!--Minimize-->
<Style x:Key="MinimizeButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
    <Setter Property="Content" Value="&#xE949;" />
    <Setter Property="Background" Value="LimeGreen" />
</Style>

<!--Maximize-->
<Style x:Key="MaximizeButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
    <Setter Property="Content" Value="&#xE739;" />
    <Setter Property="Background" Value="Orange" />
</Style>

<!--Restore-->
<Style x:Key="RestoreButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
    <Setter Property="Content" Value="&#xE923;" />
    <Setter Property="Background" Value="Orange" />
    <Setter Property="Visibility" Value="Collapsed" />
</Style>

<!--Close-->
<Style x:Key="CloseButton" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButton}">
    <Setter Property="Content" Value="&#xE106;" />
    <Setter Property="Background" Value="Firebrick" />
</Style>

I then call it from the main file with:

 <Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/UI/Styles/TitleBarButtonStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

And on the buttons with:

<Button Style="{StaticResource MinimizeButton}"
                    Grid.Column="2" 
                    Click="MinimizeBtn_OnClick" />

<Button x:Name="MaximizeBtn" Grid.Column="3" 
                    Style="{StaticResource MaximizeButton}" 
                    Click="MaximizeBtn_OnClick" />
<Button x:Name="RestoreBtn" Grid.Column="3" 
                    Style="{StaticResource RestoreButton}" 
                    Click="RestoreBtn_OnClick" />

<Button Style="{StaticResource CloseButton}" 
                    Grid.Column="4" 
                    Click="CloseBtn_OnClick" />

But on all buttons I get the error: Ressource 'STYLE_X_KEY_NAME' is not found (STYLE_X_KEY_NAME being the respective x:Key of the Style I wanted to apply)

Edit
Both files are in the same assembly.
I already tried the pack uri approach with:

<ResourceDictionary Source="pack://application:,,,/VRChat Bases Manager;component/UI/Styles/TitleBarButtonStyles.xaml" />

It still shows the same error.

Edit 2
The folderstructure here is as follows:

Main file: \UI\Windows\PrimaryWindow.xaml
The Style File: \UI\Styles\TitleBarButtonStyles.xaml

Edit 3
For some reason my file \UI\Styles\RoundDropDownStyle.xaml that gets called in the same way works.

Here the file:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ComboboxTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <Border CornerRadius="8, 0, 0, 8" BorderThickness="1,1,0,1" Background="{TemplateBinding Background}" BorderBrush="#2A2A2A">
                        <ScrollViewer x:Name="PART_ContentHost" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ComboboxButtonStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border Background="{TemplateBinding Background}" x:Name="border" CornerRadius="0,8,8,0" BorderThickness="0,1,1,1" BorderBrush="#2A2A2A">
                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ComboboxStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="SelectedIndex" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition MaxWidth="18" />
                    </Grid.ColumnDefinitions>
                    <TextBox Background="{TemplateBinding Background}" Name="PART_EditableTextBox" Style="{StaticResource ComboboxTextBoxStyle}" Padding="5,0,0,0" Height="{TemplateBinding Height}" IsReadOnly="True" />
                    <ToggleButton Background="{TemplateBinding Background}" Grid.Column="1" Margin="0" Height="{TemplateBinding Height}" Style="{StaticResource ComboboxButtonStyle}" Focusable="False" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
                        <Path Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="DodgerBlue" />
                    </ToggleButton>
                    <ContentPresenter Grid.Column="0" Name="ContentSite" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0,0,0" />
                    <Popup Grid.Column="0" Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
                        <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border Background="{TemplateBinding Background}" x:Name="DropDownBorder" BorderThickness="1" CornerRadius="5" BorderBrush="#2A2A2A" />
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

CodePudding user response:

Your Path should be,

<ResourceDictionary x:Name="TitleBarButtonStyles" Source="../Styles/TitleBarButtonStyles.xaml" />

As for your pack uri approach, you have spaces in URI. Consider Handling spaces in Uri path

CodePudding user response:

Okay I found the problem: It was the build action property on the file.
It was set to AdditionalFiles. The file that was working had it set to Page. Once I changed that it worked like a charm.

Apparently it's a weird behaviour by Visual Studio 2022?
When you create a file by Right Click -> Add... -> New Item... -> Resource Dictionary (WPF) it works fine.
If you go Right Click -> Add.. -> New From Template... -> Resource Dictionary it has the build action set to AdditionalFiles which doesn't work.

  • Related