wpf 找不到名为“关闭按钮”的资源。资源名称区分大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31609154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:18:06  来源:igfitidea点击:

Cannot find resource named 'CloseButton'. Resource names are case sensitive

c#wpf

提问by iJade

I'm trying to use Growl Alike WPF Notificationin another WCF application. But I'm getting an error when trying to run it. The errorthat I'm getting is :

我正在尝试在另一个 WCF 应用程序中使用Growl Alike WPF Notification。但是我在尝试运行它时遇到错误。我得到的错误是:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

在“System.Windows.Markup.StaticResourceHolder”上提供值引发异常。

When I check the inner exception, it says

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

Cannot find resource named 'CloseButton'. Resource names are case sensitive.

找不到名为“关闭按钮”的资源。资源名称区分大小写。

But when I check the ButtonStyle.xaml it has the resource 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>

Does any one have a clue what is it that I'm missing??

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

NOTE ::Source code works fine when run as a individual project

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

回答by Glen Thomas

In order to use StaticResource, the style has to be defined before the element you are trying to apply it to within the same visual tree.

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

You could add your resource dictionary to a control's resources above the button in the visual tree

您可以将资源字典添加到可视化树中按钮上方的控件资源中

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

or instead you could use DynamicResource, which does not require the style to be at the same level or above in the visual tree.

或者,您可以使用DynamicResource,它不需要样式在可视化树中处于同一级别或更高级别。

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

回答by hyperneu

example

例子

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

maybe this could be.use Pack URIs load files

也许这可能是。使用 Pack URIs 加载文件

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

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

回答by John C

XAML is cached. VS locks it when you have the *.xaml and *xaml.cs files open. Close them in Visual Studio and you may see the problem go away.

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