簡體   English   中英

C#中的WPF頁面導航

[英]WPF page navigation in c#

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

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

請幫忙....

您需要在主窗口中有一個框架,在該框架中將托管頁面的內容。 如果將以下名稱空間添加到MainWindow:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM