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

Why does adding ContentControl cause my application to enter break mode?

简介

我创建了一个漂亮的 WindowChrome 样式来应用到我的 windows。然而,当我将 ContentControl 添加到我的样式时,应用程序进入中断模式。

我将 this youtube video, this article, this SO question and Microsoft's documentation 中的代码拼凑在一起,得出以下代码。

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


代码

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

Icons.xaml

Window.xaml 通过 App.xaml.[=28= 引用此文件的 ContentControl Template 属性值]

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

Window.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 行),将收到以下消息。当同一行 removed/commented 退出应用程序 运行s 而不进入 中断模式 .

查看输出 window 我收到以下消息:

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. 如何解决这个问题?
    • 请注意,我必须使用 Icons.xaml 文件中的 ControlTemplate,并且对该内容的调用必须保留在 window 的 Style 中(并且不是 window 的实际 xaml)。

简介

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

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

代码

我的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"/>