简体   繁体   English

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

[英]WPF Page Navigation C# Not Working

I am very new to C# and the WPF Architecture. 我对C#和WPF架构非常陌生。 I am trying to create an application with simple page Navigation. 我正在尝试使用简单的页面导航创建应用程序。 I have changed my MainWindow from Window to NavigationWindow. 我已经将MainWindow从Window更改为NavigationWindow。 I have also put the following code in MainWindow.xaml.cs 我也将以下代码放在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");
        }
    }

I am calling this method from the default source ("HubView.xaml") this is in a folder called "Pages". 我从默认源(“ HubView.xaml”)调用此方法,该源位于名为“页面”的文件夹中。 The code looks like this 代码看起来像这样

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

However when I click the button the message box is displayed indicating that nav equals null. 但是,当我单击按钮时,将显示消息框,指示nav等于null。 I have read through this question and the subsequent answer, however I still do not fully understand. 我已经阅读了这个问题和后续的答案,但是我仍然不完全理解。

Any suggestions would be greatly appreciated! 任何建议将不胜感激!


Edit: 编辑:

This is the "MainWindow.xaml": 这是“ 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>

As you can see the Window is a Navigation Window. 如您所见,该窗口是一个导航窗口。

NavigationService is for page navigation in WPF like in a browser and for that to work you would need to add a Frame to your MainWindow.xaml 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>

and use NavigationService to navigate eg 并使用NavigationService进行导航

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

EDIT Create 1 window and 2 pages 编辑创建1个窗口和2个页面

MainWindow.xaml 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 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 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 has a property of NavigationService so no need to create new variables. NavigationWindow具有的属性NavigationService所以没必要创建新的变量。 Hope that helps. 希望能有所帮助。 NOTE Page2.xaml is just a blank page 注意 Page2.xaml只是一个空白页

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

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