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

时间:2015-07-24 11:15:34

标签: c# wpf

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

  

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

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

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

3 个答案:

答案 0 :(得分:5)

为了使用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>

答案 1 :(得分:1)

XAML已缓存。打开* .xaml和* xaml.cs文件时,VS会锁定它。在Visual Studio中将其关闭,您可能会发现问题消失了。

答案 2 :(得分:0)

示例

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

也许是这样。使用Pack URI加载文件

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