简体   繁体   中英

WPF page navigation in c#

I am trying to learn c# and WPF application. Here I am trying to redirect from one WPF page(MainWindow.xaml) to another(HandWash.xaml) on a button click event. But the following code is throwing NULLReferenceException. This is the MainWindow.xaml file.

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

Background code for this:

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);
        }
    }

This is the code for 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>

and the background code is:

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

Please help....

You need to have a frame in main window where contents of Pages will be hosted. If you add the following namespace to MainWindow:

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

you can define a frame somewhere, eg in your grid:

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

Then you can navigate from your event handler like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM