2014-01-14 106 views
1

我想學習c#和WPF應用程序。在這裏,我試圖在一個按鈕單擊事件上從一個WPF頁面(MainWindow.xaml)重定向到另一個(HandWash.xaml)。但是下面的代碼拋出了NULLReferenceException。 這是MainWindow.xaml文件。此c#中的WPF頁面導航#

<Window x:Class="MyApplication.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    d:DesignHeight="720" d:DesignWidth="1284" 
    Title="StartPage" WindowStartupLocation="CenterScreen" WindowStyle="None" WindowState="Maximized" Closed="Window_Closed"> 
<Window.Background> 
    <ImageBrush ImageSource="/Images/StartPage.png"></ImageBrush> 
</Window.Background> 
<Grid> 
    <Button Content="Hand Wash" Height="794" HorizontalAlignment="Left" Name="HandWash" VerticalAlignment="Top" Width="353" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="HandWash_Click"/> 
    <Button Content="Bathing" Height="794" HorizontalAlignment="Left" Margin="390,0,0,0" Name="Bathing" VerticalAlignment="Top" Width="301" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="Bathing_Click"/> 
    <Button Content="Nail-Clip" Height="794" HorizontalAlignment="Left" Margin="730,0,0,0" Name="NailClip" VerticalAlignment="Top" Width="295" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="NailClip_Click"/> 
    <Button Content="Teeth Brush" Height="794" HorizontalAlignment="Left" Margin="1067,0,0,0" Name="TeethBrush" VerticalAlignment="Top" Width="310" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="TeethBrush_Click"/> 
</Grid> 
</Window> 

背景代碼:

private void TeethBrush_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      TeethBrush teeth = new TeethBrush(myarg); 
      NavigationService navService = NavigationService.GetNavigationService(this); 
      navService.Navigate(teeth);  // NULL REFERENCE EXCEPTION at this line 
     } 
     catch (NullReferenceException ex) 
     { 
      System.Windows.MessageBox.Show(ex.Message); 
     } 
    } 

這是TeethBrush.xaml代碼:

<Page x:Class="MyApplication.TeethBrush" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    d:DesignHeight="720" d:DesignWidth="1284" 
    Title="TeethBrush"> 

<Grid> 

</Grid> 
<Page.Background> 
    <ImageBrush ImageSource="C:\Users\Tonmoy\Documents\Visual Studio 2010\Projects\MyKinectApp\MyKinectApp\Images\StartPage.png"></ImageBrush> 
</Page.Background> 

</Page> 

和後臺代碼:

public TeethBrush(Myargs arg) 
    { 
     InitializeComponent(); 
     //Rest of the code 
    } 

請幫助......

+0

是'navService'初始化? – Loetn

+0

是navService爲空......我該如何解決這個問題? – Tonmoy

+0

什麼是'NavigationService'? – Loetn

回答

1

您需要在主窗口中包含頁面內容的框架。 如果將以下命名空間添加到主窗口:

xmlns:local="clr-namespace:System.Windows.Controls;assembly=PresentationFramework" 

可以某處定義的框架,例如在網格:

<Grid> 
    <local:Frame x:Name="mainFrame">    
    </local:Frame> 
    .... 

然後你可以從你的事件處理程序,導航,像這樣:

TeethBrush teeth = new TeethBrush(myarg); 
this.mainFrame.Navigate(teeth); 
+0

我試過這個......但是兩個WPF頁面相互重疊。我只想查看下一頁及其相應的功能。 – Tonmoy