当我在按钮上使用样式或模板时,为什么我的窗口不会加载?

时间:2017-05-24 11:34:50

标签: wpf xaml

我是WPF的新手,所以这让我很沮丧。

考虑我的xaml标记中的以下按钮:

<Button Name="btnSMS" Click="btnSMS_Click" Height="30" Width="66"
    Margin="10,20,10,10" Background="#FF1E8383" Foreground="White"
    Template="{StaticResource RoundedButtonGreen}">Send SMS</Button>

我已经定义了它正在使用的模板如下(这个想法实际上只是为了获得圆角):

<Window.Resources>
    <ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
        <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
        </Border>
    </ControlTemplate>
</Window.Resources>

Window.Resources中,我也有一个我定义的Style,基本上做同样的事情:

    <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
        <Setter Property="Background" Value="#FF1E8323" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>-->

因此,当我在Visual Studio中点击Debug时,我收到以下错误 IF ,并且仅当按钮应用了样式或模板时才会出现。

没有样式或模板,窗口加载完全正常。 enter image description here

修改

按要求,这是整个窗口

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SCADA_Demo"
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:dxsch="http://schemas.devexpress.com/winfx/2008/xaml/scheduler" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="SCADA_Demo.MainWindow"
        mc:Ignorable="d"
        Title="IHS Towers SCADA" WindowStyle="None" Background="#1e1e1e"
    Loaded="Window_Loaded">

    <Grid>
        <DockPanel LastChildFill="True">
            <!-- Title Bar -->
            <DockPanel LastChildFill="True" DockPanel.Dock="Top" Background="#2d2d30">
                <StackPanel DockPanel.Dock="Left" Height="30" Background="#2d2d30" FlowDirection="LeftToRight" Orientation="Horizontal">
                    <Label Name="lblTitle" HorizontalAlignment="Left" BorderThickness="0" Foreground="White">IHS Towers SCADA</Label>
                </StackPanel>
                <StackPanel DockPanel.Dock="Right" Height="30" Background="#2d2d30" FlowDirection="RightToLeft" Orientation="Horizontal">
                    <Button x:Name="btnClose" Content="X" Click="btnClose_Click" Width="30" Foreground="White"  Background="#2d2d30" BorderThickness="0"></Button>
                    <Button Name="btnMinimize" Content="_" Click="btnMinimize_Click" Width="30" Foreground="White" Background="#2d2d30" BorderThickness="0"></Button>
                </StackPanel>
            </DockPanel>

            <!-- Towers Bar across the top -->
            <StackPanel DockPanel.Dock="Top" Height="50" Background="#2d2d30" VerticalAlignment="Top" x:Name="spTowers" Orientation="Horizontal">
                <Rectangle Name="Tower1" Fill="Green" Height="30" Width="30" Margin="10,5,10,5"></Rectangle>
                <Rectangle Name="Tower2" Fill="Red" Height="30" Width="30" Margin="10,5,10,5"></Rectangle>
            </StackPanel>

            <!-- Alert Window in the center -->
            <StackPanel Name="spBody" DockPanel.Dock="Top" Height="396" Background="#b6bcc6"></StackPanel>

            <!-- Actions Bar across the bottom -->
            <StackPanel DockPanel.Dock="Bottom" Height="60" Background="#2d2d30" VerticalAlignment="Bottom" x:Name="spActions" FlowDirection="RightToLeft" Orientation="Horizontal">

                <Button Name="btnSMS" Click="btnSMS_Click" Height="30" Width="66" Margin="10,20,10,10" Background="#FF1E8383" Foreground="White" Template="{StaticResource RoundedButtonGreen}">Send SMS</Button>
            </StackPanel>
        </DockPanel>
    </Grid>

    <Window.Resources>
        <ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
            <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
            </Border>
        </ControlTemplate>
        <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
            <Setter Property="Background" Value="#FF1E8323" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>-->
    </Window.Resources>

</Window>

2 个答案:

答案 0 :(得分:9)

您应该在网格之前定义<Window.Resources> 。订单很重要:

<Window ...>
    <Window.Resources>
        <ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
            <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
            </Border>
        </ControlTemplate>
        <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
            <Setter Property="Background" Value="#FF1E8323" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>-->
    </Window.Resources>
    <Grid>
    ...
</Window>

答案 1 :(得分:2)

您应该将Window.Resources放在XAML中的Grid之前。

StaticResource是一个MarkupExtension,当XAML解析时,它会正确获取资源 - 因此XAML中的外观顺序非常重要。没有前瞻性声明;资源必须已经定义。

From MSDN

  

StaticResource不得尝试对在XAML文件中进一步词法定义的资源进行前向引用。不支持尝试这样做,并且即使这样的引用没有失败,当搜索表示ResourceDictionary的内部哈希表时,尝试前向引用将导致加载时间性能损失。为获得最佳结果,请调整资源字典的组成,以避免前向引用。如果您无法避免转发引用,请改用DynamicResource标记扩展。