如何在 WPF 中从一页导航到另一页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28576178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Navigate from One Page to Another in WPF
提问by Simsons
I have a MainWindow.XAMLand CustomersView.XAML.
我有一个MainWindow.XAML和CustomersView.XAML。
When I click the Customer Button on MainWindow , I want to navigate to CustomersView.XAMLand palong with that need to pass few parameters.
当我单击 MainWindow 上的 Customer 按钮时,我想导航到CustomersView.XAML需要传递几个参数的地方。
I can use NavigationServicebut is only available with Pages and not Window.Hyperlink is not an option at this moment.
我可以使用NavigationService但仅适用于 Pages 而不适用于 Window.Hyperlink 目前不是一个选项。
This might be fairly simple thing but not sure how can I implement this using MVVM and with out any third party control.
这可能是相当简单的事情,但不知道如何使用 MVVM 并且没有任何第三方控制来实现这一点。
回答by Prince Jain
private void Navigate_Click(object sender, RoutedEventArgs e)//By Prince Jain
{
this.NavigationService.Navigate(new Uri("Page3.xaml", UriKind.Relative));
}
回答by Dhru 'soni
Simple Way=> Xaml:
简单方式=> Xaml:
<Button HorizontalAlignment="Right" Name="continueButton" Width="75" Margin="0,0,8,11" Height="23" VerticalAlignment="Bottom" Click="continueButton_Click">
Navigate
</Button>
C#:
C#:
private void continueButton_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.GoForward();
//or
this.NavigationService.Navigate("Second.xaml")
}
In Mvvm XAML:
在 Mvvm XAML 中:
<Button Command="{x:Static Views:Commands.NavigateHelp}" Content="Help"/>
In Views (We have a Commands.cs file that contains all of these):
在视图中(我们有一个包含所有这些的 Commands.cs 文件):
public static RoutedCommand NavigateHelp = new RoutedCommand();
In the Page contstructor, you can connect the two:
在 Page 构造函数中,您可以将两者连接起来:
CommandBindings.Add(new CommandBinding(Commands.NavigateHelp, NavigateHelpExecute));
NavigateHelpExecute can be in the code behind (which is what we do), hook into a ViewModel event handler, or whatever. The beauty of this is that you can disable other navigation like so:
NavigateHelpExecute 可以在后面的代码中(这就是我们所做的),挂钩到 ViewModel 事件处理程序,或其他任何东西。这样做的好处是您可以像这样禁用其他导航:
CommandBindings.Add(new CommandBinding(NavigationCommands.Refresh, null));
回答by Jawahar
There are many options to navigate from one window to another in WPF. You can use a Frame in your MainWindow and navigate all your pages right inside your Frame.
在 WPF 中有许多选项可以从一个窗口导航到另一个窗口。您可以在 MainWindow 中使用 Frame 并在 Frame 中导航所有页面。
<Window
x:Class="NavigationSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
>
<DockPanel>
<Frame x:Name="_mainFrame" />
</DockPanel>
</Window>
From code, you can tell the frame to navigate, like so:
从代码中,您可以告诉框架进行导航,如下所示:
_mainFrame.Navigate(new Page1());
Which just so happens to be a helpful shortcut to:
这恰好是一个有用的快捷方式:
_mainFrame.NavigationService.Navigate(new Page1());
Or if you using any framework like PRISM, you are allowed to create a Shell where you can define regions and let your pages navigate to that.
或者,如果您使用任何框架(如 PRISM),则可以创建一个 Shell,您可以在其中定义区域并让您的页面导航到该区域。

