c# - 为什么添加 ContentControl 会导致我的应用程序进入中断模式?

标签 c# wpf xaml contentcontrol window-chrome

<分区>

简介

我创造了一个美丽的 WindowChrome应用于我的窗口的样式。当我添加 ContentControl然而,按照我的风格,应用程序进入中断模式。

我拼凑了来自 this youtube video 的代码, this article , this SO questionMicrosoft's documentation我想出了以下代码。

注意:下面的代码都被认为是相关,因为应用程序不能与这些部分中的任何一个一起运行(是的,我知道它可以在没有代码隐藏的情况下运行,但是不得不从 Visual Studio 而不是关闭按钮停止应用程序很烦人——这也是我想要完成的)。实际上,我已经精简了下面的代码,以便更容易使用。


代码

窗口.xaml

<Style x:Key="TestWindow" TargetType="{x:Type Window}">
    <Setter Property="Background" Value="#FF222222"/>
    <Setter Property="BorderBrush" Value="WhiteSmoke"/>
    <Setter Property="BorderThickness" Value="5,30,5,5"/>
    <Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome CaptionHeight="20"
                          CornerRadius="0"
                          GlassFrameThickness="0,0,0,-1"
                          NonClientFrameEdges="None"
                          ResizeBorderThickness="5"
                          UseAeroCaptionButtons="True"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">

                <Grid>

                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Border>

                    <DockPanel LastChildFill="True" VerticalAlignment="Top" Height="30">

                        <StackPanel DockPanel.Dock="Right"
                                    Orientation="Horizontal"
                                    VerticalAlignment="Center">
                            <Button x:Name="Button_Close"
                                    WindowChrome.IsHitTestVisibleInChrome="True"
                                    Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
                                    Click="CloseClick">
                                <ContentControl Template="{StaticResource Icon_Close}" Height="10"/>
                            </Button>
                        </StackPanel>

                        <StackPanel DockPanel.Dock="Left"
                                    Orientation="Horizontal"
                                    VerticalAlignment="Center">
                            <Image x:Name="PART_WindowCaptionIcon"
                                   Width="16"
                                   Height="16"
                                   Margin="0,0,6,0"
                                   Source="{TemplateBinding Icon}"/>
                            <TextBlock x:Name="PART_WindowCaptionText"
                                       Margin="6,0,0,0"
                                       Padding="0">
                                <Run BaselineAlignment="Center"
                                     Text="{TemplateBinding Title}"
                                     Foreground="Black"/>
                            </TextBlock>
                        </StackPanel>

                    </DockPanel>

                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger SourceName="PART_WindowCaptionIcon" Property="Source" Value="{x:Null}">
                        <Setter TargetName="PART_WindowCaptionIcon" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="PART_WindowCaptionText" Property="Margin" Value="5,0,0,0"/>
                    </Trigger>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

图标.xaml

Window.xamlContentControl 引用此文件Template通过 App.xaml 的属性值.

<ControlTemplate x:Key="Icon_Close">
    <Viewbox>
        <Polygon Points="357,35.7 321.3,0 178.5,142.8 35.7,0 0,35.7 142.8,178.5 0,321.3 35.7,357 178.5,214.2 321.3,357 357,321.3 214.2,178.5" Fill="Black"/>
    </Viewbox>
</ControlTemplate>

窗口.xaml.cs

public partial class Window : ResourceDictionary
{
    public Window()
    {
        InitializeComponent();
    }

    private void CloseClick(object sender, RoutedEventArgs e)
    {
        var window = (System.Windows.Window)((FrameworkElement)sender).TemplatedParent;
        window.Close();
    }
}

问题

当行<ContentControl Template="{StaticResource Icon_Close}" Height="10"/>存在(第 38 行),收到以下消息。当同一行被删除/注释掉时,应用程序运行时不会进入中断模式

Message: The application is in break mode

查看“输出”窗口时,我收到以下消息:

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

问题

此代码直接放在 Window 的 XAML 代码中时有效, 但当我尝试将其放入模板时失败了。

我的问题是:

  1. 为什么我的应用程序在 ContentControl 时进入中断模式放在 Window 中的模板?
  2. 我该如何解决这个问题?
    • 请注意,我必须使用我的 ControlTemplate来自 Icons.xaml文件并且对此内容的调用必须保留在窗口的 Style 中(而不是窗口的实际 xaml)。

最佳答案

简介

问题是由于我的样式顺序不正确,根据 the answerthis question .我将我的问题标记为那个问题的副本,但我觉得我应该分享这个作为答案,以防它对其他人有帮助。

我喜欢 Microsoft 没有以适当的异常(exception)处理此问题,并且您需要用头撞墙,直到您的头或墙壁破裂。

代码

我的 App.xamlResourceDictionary.MergedDictionaries 中包含以下代码

<ResourceDictionary Source="pack://application:,,,/MyProject;component/Window.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MyProject;component/Icons.xaml"/>

我把顺序改成了下面的

<ResourceDictionary Source="pack://application:,,,/MyProject;component/Icons.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MyProject;component/Window.xaml"/>

关于c# - 为什么添加 ContentControl 会导致我的应用程序进入中断模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47823738/

相关文章:

c# - WPF:如何自定义通用自定义窗口?

c# - 如何将可写位图的一部分复制到另一个可写位图?

c# - 与另一个读取输入在同一行读取输入

c# - 使用 XNA 3.0 的软阴影技术

c# - Windows 服务 : using of BitmapEncoder or BitmapDecoder ends with «The operation completed successfully»

c# - SemanticZoom 中的 UWP GridView ItemTemplateSelector 不适用

c# - linq 连接错误

wpf - 通过 XAML 在 WPF 中获取焦点元素

c# - 是否有可能通过 RIA 服务获得类似 0..1 数据的关联?

windows-phone-7 - 我在哪里可以找到静态资源?