WPF:XAML分析异常未处理

本文关键字:异常 未处理 XAML WPF | 更新日期: 2023-09-27 17:59:27

我熟悉了WinForms,我正在尝试学习一些WPF的东西。

在初始化窗口时,我遇到了此XAML分析异常。以下是我迄今为止的一些调查。

首先,我有两节课,分别命名为"问题"answers"答案"。

当我尝试初始化这两个类时,我得到了错误,如下所示:

public MainWindow()
{
    InitializeComponent();
}   
private string _firstName;
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}
Question _question = new Question();
Answer _answer = new Answer();
private void MetroWindow_Initialized_1(object sender, EventArgs e)
{
}
private void MetroWindow_Loaded_1(object sender, RoutedEventArgs e)
{
    //some code here
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
    //some code here
}

但当我尝试删除问答的初始化时,它运行正常。

顺便说一句,我正在使用MahApps。

有人能就我的问题给我一个提示吗?非常感谢!

[编辑]

它说是

The invocation of the constructor on type 'Recitation_Game.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9

你可能想看看我的xaml,它是:

<Controls:MetroWindow x:Class="Recitation_Game.MainWindow" ShowIconOnTitleBar="true"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        Title="Recitation Game" Height="350" Width="525" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" Initialized="MetroWindow_Initialized_1" Loaded="MetroWindow_Loaded_1">
    <Grid>
        <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="178" Margin="10,91,0,0" VerticalAlignment="Top" Width="497" Stroke="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
        <Label x:Name="lblWelcome" Content="Label" HorizontalAlignment="Left" Margin="30,109,0,0" VerticalAlignment="Top" Foreground="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" FontSize="14"/>
        <Label Content="Recitation Game" HorizontalAlignment="Left" Margin="366,23,0,0" VerticalAlignment="Top" FontSize="18">
            <Label.Foreground>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.MenuHighlightColorKey}}"/>
            </Label.Foreground>
        </Label>
        <Label Content="v. 01.00.00" HorizontalAlignment="Left" Margin="432,57,0,0" VerticalAlignment="Top" FontSize="14">
            <Label.Foreground>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.MenuHighlightColorKey}}"/>
            </Label.Foreground>
        </Label>
        <Button x:Name="btnStart" Content="Start Game" HorizontalAlignment="Left" Margin="400,275,0,0" VerticalAlignment="Top" Width="107" Click="btnStart_Click"/>
    </Grid>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/FlatButton.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
</Controls:MetroWindow>

再次感谢!

WPF:XAML分析异常未处理

I怀疑问题是XAML试图使用Window.Loaded事件处理程序,但没有(至少在发布的代码中没有)。

这是xaml:的第4行

 Loaded="MetroWindow_Loaded_1"

也就是说,XAML解析异常在消息中没有提供太多信息。但是,您可以检查异常的InnerException,通常可以获得有关XAML解析器失败原因的更多详细信息。