使用 ResourceDictionary 导致找不到资源“xxx”

我目前正在尝试通过在有意义时将其拆分为多个文件来组织我的 UI Xaml。

因此,我将我的 TitleBar 样式导出到一个额外的文件 ( UIStylesTitleBarButtonStyles.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>

然后我从主文件中调用它:

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

在按钮上:

<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" />

但是在所有按钮上我都收到错误: Ressource 'STYLE_X_KEY_NAME' is not found (STYLE_X_KEY_NAME 是我想应用的样式的相应 x:Key)

编辑
这两个文件都在同一个程序集中。
我已经尝试了 pack uri 方法:

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

它仍然显示相同的错误。

编辑 2
这里的文件夹结构如下:

主文件: UIWindowsPrimaryWindow.xaml
样式文件: UIStylesTitleBarButtonStyles.xaml

编辑 3
出于某种原因,我的文件 UIStylesRoundDropDownStyle.xaml 以相同的方式被调用。

这里的文件:

<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>
stack overflow Using ResourceDictionary results in Resource 'xxx' not found
原文答案
author avatar

接受的答案

好的,我发现了问题:这是文件上的构建操作属性。
它被设置为 AdditionalFiles 。正在运行的文件将其设置为 Page 。一旦我改变它,它就像一个魅力。

显然这是 Visual Studio 2022 的一种奇怪行为?
当您通过 Right Click -> Add... -> New Item... -> Resource Dictionary (WPF) 创建文件时,它工作正常。
如果你去 Right Click -> Add.. -> New From Template... -> Resource Dictionary 它的构建操作设置为 AdditionalFiles 这不起作用。

编辑 我认为 New from Template... 实际上是 ReSharper 添加的功能。


答案:

作者头像

你的路径应该是,

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

至于您的 pack uri 方法,您在 URI 中有空格。考虑 Handling spaces in Uri path