【问题标题】:WPF: XAML Parse Exception was unhandledWPF:未处理 XAML 解析异常
【发布时间】:2015-04-24 23:12:31
【问题描述】:

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

当窗口初始化时,我遇到了这个 XAML 解析异常。以下是我目前所做的一些调查。

首先,我有 2 个名为“问题”和“答案”的课程。

当我尝试像这样初始化这两个类时出现错误:

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>

再次感谢!

【问题讨论】:

  • XAML 解析异常。 C# 代码无济于事。发布您的 XAML。
  • 另外,发布特定的 XAML 异常文本 - 它通常有一个行号
  • I familiarized myself in WinForms, and I am trying to learn some WPF stuff.。第一步:忘记你在winforms中学到的一切。 WPF 需要不同的心态。
  • 另外,WPF 中没有 Form 这样的东西。有Windows,如果你在乎的话。
  • 由于您来自 Windows 窗体,您可能想看看:reedcopsey.com/series/windows-forms-to-mvvm 这是我写的关于从 WinForms 迁移到 WPF 思维方式的系列...

标签: c# wpf xaml


【解决方案1】:

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

这是在 xaml 的第 4 行:

 Loaded="MetroWindow_Loaded_1"

话虽如此,XAML 解析异常并未在其消息中提供大量信息。但是,您可以检查异常的 InnerException,并且通常会获得有关 XAML 解析器失败原因的更多详细信息。

【讨论】:

  • 我在 'MetroWindow_Loaded_1' 后面的实际代码中得到了这个事件,很抱歉没有放这个。请看一下我的编辑,谢谢!
  • @JRC 你能发布你的解析异常的内部异常吗?
  • 我试图在 InitializeComponent() 中设置一个断点,但在它到达断点之前它已经抛出一个错误并且“没有可用的源”
  • @JRC 当您运行并收到异常时,您应该能够在此时检查详细信息。 (没有断点)
  • @JRC 没问题 - 对于 XAML 异常,您总是需要检查 InnerExceptions 中的细节 - 有时需要很多层级。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
相关资源
最近更新 更多