2013-02-15 38 views
3

我熟悉了WinForms,我正在嘗試學習一些WPF的東西。WPF:XAML解析異常未處理

當窗口正在初始化時,我遇到了這個XAML解析異常。以下是我迄今爲止的一些調查。

首先,我有2個名爲'問題'和'答案'的課程。

我得到的錯誤,當我試圖初始化這些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> 

謝謝!

+0

XAML解析異常。 C#代碼不會幫助。發佈您的XAML。 – 2013-02-15 16:57:40

+0

另外,發佈特定的XAML異常文本 - 它通常有一個行號 – 2013-02-15 16:58:06

+0

'我在WinForms中熟悉了自己,並且我正在嘗試學習一些WPF的東西。第一步:忘記你在winforms中學到的所有東西。 WPF需要不同的心態。 – 2013-02-15 16:58:09

回答

3

嫌疑人問題是您的XAML嘗試使用Window.Loaded事件處理程序,但沒有一個(至少不在發佈的代碼中)。

這是XAML中的第4行:

Loaded="MetroWindow_Loaded_1" 

話雖這麼說,XAML解析異常不提供大量的信息在他們的消息。但是,您可以檢查異常的InnerException,並且通常會獲得有關XAML解析器失敗原因的更多詳細信息。

+1

我在背後的MetroWindow_Loaded_1背後的實際代碼中收到了此事件。請看看我的編輯,謝謝! – jomsk1e 2013-02-15 17:06:36

+0

@JRC你可以發佈解析異常的內部異常嗎? – 2013-02-15 17:11:37

+1

我試圖在InitializeComponent()中設置一個斷點,但在它到達斷點之前它已經拋出一個錯誤,並且'沒有可用的源' – jomsk1e 2013-02-15 17:17:51