繁体   English   中英

WPF页面导航C#无法正常工作

[英]WPF Page Navigation C# Not Working

我对C#和WPF架构非常陌生。 我正在尝试使用简单的页面导航创建应用程序。 我已经将MainWindow从Window更改为NavigationWindow。 我也将以下代码放在MainWindow.xaml.cs中

 public void View(string newView) 
    {
        Uri view = new Uri(newView, UriKind.Relative);
        NavigationService nav = NavigationService.GetNavigationService(this);
        if (nav != null) 
        {   
            nav.Navigate(view);
        }
        else
        {
            MessageBox.Show("NavNull");
        }
    }

我从默认源(“ HubView.xaml”)调用此方法,该源位于名为“页面”的文件夹中。 代码看起来像这样

 public void Button_Click(object sender, RoutedEventArgs e)
    {
        View = @"\Pages\UserAdd.xaml";
        MainWindow mainWindow = new MainWindow();
        mainWindow.View(View);
     }

但是,当我单击按钮时,将显示消息框,指示nav等于null。 我已经阅读了这个问题和后续的答案,但是我仍然不完全理解。

任何建议将不胜感激!


编辑:

这是“ MainWindow.xaml”:

<NavigationWindow x:Class="Container.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ResizeMode="CanMinimize"
    Title="MainWindow" Height="350" Width="525" Source="Pages/HubView.xaml" WindowStyle="ThreeDBorderWindow" ShowsNavigationUI="False">
    </NavigationWindow>

如您所见,该窗口是一个导航窗口。

NavigationService用于在WPF中像在浏览器中一样进行页面导航,要使其正常工作,您需要将一个Frame添加到MainWindow.xaml

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

    <Button x:Name="btnPage1" Content="Page 1" Width="200" Height="50"
            Click="btnPage1_Click"/>        
</Grid>

并使用NavigationService进行导航

private void btnPage1_Click(object sender, RoutedEventArgs e)
{
    mainFrame.NavigationService.Navigate(new Uri("Page1.xaml", UriKind.Relative));
}

编辑创建1个窗口和2个页面

MainWindow.xaml

<NavigationWindow x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Source="Page1.xaml">

Page1.xaml

<Page x:Class="WpfApplication1.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">

<Grid>
    <TextBlock Text="Page 1" FontWeight="Bold" />
    <Button Margin="0,40" Content="Goto Page 2" Width="200" Height="50"
            Click="Button_Click"/>
</Grid>

Page1.xaml.cs

public partial class Page1 : Page
{
    public Page1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
    }
}

NavigationWindow具有的属性NavigationService所以没必要创建新的变量。 希望能有所帮助。 注意 Page2.xaml只是一个空白页

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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