找不到名为'CloseButton'的资源.资源名区分大小写

本文关键字:资源 名区 大小写 找不到 CloseButton | 更新日期: 2023-09-27 18:04:22

我试图在另一个WCF应用程序中使用Growl Alike WPF通知。但我得到一个错误,当试图运行它。我得到的错误是:

在"System.Windows.Markup"上提供值。StaticResourceHolder'抛出异常。

当我检查内部异常时,它说

无法找到名为"CloseButton"的资源。资源名区分大小写。

但是当我检查ButtonStyle。xaml有资源CloseButton .

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="NormalBorderBrush" Color="Black" />
<SolidColorBrush x:Key="DefaultedBorderBrush" Color="Black" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />

<LinearGradientBrush x:Key="CloseNormal" StartPoint="0.5,0" EndPoint="0.5,1">
    <GradientStop Color="#394452" Offset="0.0"/>
    <GradientStop Color="#343e4a" Offset="1.0"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="CloseOver" StartPoint="0.5,0" EndPoint="0.5,1">
    <GradientStop Color="#515a6b" Offset="0.0"/>
    <GradientStop Color="#474f5d" Offset="1.0"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ClosePressed" Color="#090909" />
<Style x:Key="ButtonFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Border>
                    <Rectangle 
        Margin="2"
        StrokeThickness="1"
        Stroke="#60000000"
        StrokeDashArray="1 2"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="CloseButton" TargetType="{x:Type Button}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="MinHeight" Value="16"/>
    <Setter Property="MinWidth" Value="16"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Border x:Name="Border" CornerRadius="3" BorderThickness="0" ClipToBounds="False" Background="{StaticResource CloseNormal}" BorderBrush="{StaticResource NormalBorderBrush}">
                        <Border.Effect>
                            <DropShadowEffect ShadowDepth="0" Opacity=".4" BlurRadius="5" Color="Black"/>
                        </Border.Effect>
                        <Grid>
                            <Image Source="pack://application:,,,/Resources/close.png" IsHitTestVisible="False" Margin="2">
                                <Image.Effect>
                                    <DropShadowEffect Direction="90" ShadowDepth="1" BlurRadius="1"/>
                                </Image.Effect>
                            </Image>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource CloseOver}" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource ClosePressed}" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="true">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

有人知道我错过了什么吗?

注意:源代码在作为单个项目运行时工作正常

找不到名为'CloseButton'的资源.资源名区分大小写

为了使用StaticResource,样式必须在您试图将其应用于同一视觉树中的元素之前定义。

您可以将您的资源字典添加到可视树

按钮上方的控件资源中。
<Window x:Class="StackOverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary Source="ButtonStyle.xaml"/>
</Window.Resources>
<Grid>
    <Button Click="ButtonBase_OnClick" Style="{StaticResource CloseButton}">Click</Button>
</Grid>

或者你可以使用DynamicResource,它不要求样式在视觉树中处于相同或更高的级别。

<Button Click="ButtonBase_OnClick" Style="{DynamicResource CloseButton}">Click</Button>

XAML被缓存。当你有*时,VS锁定它。打开Xaml和* Xaml .cs文件。在Visual Studio中关闭它们,你可能会看到问题消失了。

示例

<Application.Resources>
    <ResourceDictionary Source="pack://application:,,,/WpfCustomControlLibrary1;component/UI/ButtonStyle.xaml" ></ResourceDictionary>
</Application.Resources>

可能是。使用包uri加载文件

https://learn.microsoft.com/en-us/dotnet/framework/wpf/app-development/pack-uris-in-wpf