3
votes

I familiarized myself in WinForms, and I am trying to learn some WPF stuff.

I am experiencing this XAML Parse Exception as the Window is initializing. Here are some investigation that I've got so far.

To start, I have 2 classes which named 'Question' and 'Answer'.

I got the error when I tried to initializes these 2 class like this:

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
}

But when I tried to remove the initialization of Question and Answer, it runs okay.

BTW I am using MahApps.

Can somebody give me a hint with my problem? Thanks a lot!

[EDIT]

It say's

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

You may want to look on my xaml here it is:

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

Thanks again!

1
XAML parse exception. C# code will not help. Post your XAML.Federico Berasategui
Also, post the specific XAML exception text - it usually has a line numberDana Cartwright
I familiarized myself in WinForms, and I am trying to learn some WPF stuff.. First Step: Forget everything you've ever learned in winforms. WPF requires a different mentality.Federico Berasategui
Also, there's no such thing as a Form in WPF. There are Windows, if you care.Federico Berasategui
Since you're coming from Windows Forms, you might want to look at: reedcopsey.com/series/windows-forms-to-mvvm It's a series I wrote about migrating to a WPF mindset from WinForms...Reed Copsey

1 Answers

3
votes

I suspect the problem is that your XAML tries to use a Window.Loaded event handler, but there isn't one (at least not in the posted code).

This is on line 4 of the xaml:

 Loaded="MetroWindow_Loaded_1"

That being said, XAML Parse Exceptions don't provide a lot of information in their message. However, you can inspect the InnerException of the exception, and typically get much more details about why the XAML parser failed.