2014-01-14 73 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