在 WPF 中从一个 xaml 导航到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24285132/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:57:07  来源:igfitidea点击:

Navigating from one xaml to another in WPF

c#wpfxaml

提问by E_learner

Despite the fact that I am totally a fresh to WPF, I need to write a code, in which after I click a button, the application should open another xaml. After searching on the web, I did in in the following way:

尽管我对 WPF 完全陌生,但我需要编写代码,在其中单击按钮后,应用程序应该打开另一个 xaml。在网上搜索后,我是通过以下方式进行的:

1.I created two xaml files, namely 'Window1.xaml' and 'Window2.xaml'.

1.我创建了两个xaml文件,分别是“Window1.xaml”和“Window2.xaml”。

2.In my 'App.xaml' file, I let:

2.在我的“App.xaml”文件中,我让:

<Application x:Class="DiagramDesigner.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    StartupUri="Window1.xaml">

3.Then in my 'Window1.xaml', I created a button:

3.然后在我的“Window1.xaml”中,我创建了一个按钮:

<Button Name="Button" Click="Button_Click_1" MouseEnter="Button_MouseEnter_1" IsDefault="True"
        HorizontalAlignment="Center" VerticalAlignment="Center">
    Start
</Button>

4.In my "Windwo1.xaml.cs" file, I created these two functions:

4.在我的“Windwo1.xaml.cs”文件中,我创建了这两个函数:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    { 
    }

    private void Button_MouseEnter_1(object sender, MouseEventArgs e)
    {
    }

5.Then for opening "Window2.xaml" after clicking the button, I changed to:

5.然后点击按钮后打开“Window2.xaml”,我改为:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    { 
        NavigationService service = NavigationService.GetNavigationService(this);
        service.Navigate(new Uri("Window2.xaml", UriKind.RelativeOrAbsolute));
    }

But this gives me error, saying the serviceis null, and the program crashed. I didn't figure out any way to solve this. Any suggestions? Thanks.

但这给了我错误,说它service为空,并且程序崩溃了。我没有想出任何方法来解决这个问题。有什么建议?谢谢。

回答by Nikita B

Try this:

尝试这个:

private void Button_Click_1(object sender, RoutedEventArgs e)
{ 
    var window = new Window2();
    window.ShowDialog();
}

You should also read the documentation on NavigationServiceclass and its methods to avoid further confusion on what this class does. Here is a good place to start: http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.getnavigationservice%28v=vs.110%29.aspx

您还应该阅读有关NavigationService类及其方法的文档,以避免进一步混淆该类的作用。这是一个很好的起点:http: //msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.getnavigationservice%28v=vs.110%29.aspx

回答by Eugene Podskal

NavigationService could not be used in classic WPF desktop application that use the Windowclass http://msdn.microsoft.com/en-us/library/ms750478(v=vs.110).aspx.

NavigationService 无法在使用Windowhttp://msdn.microsoft.com/en-us/library/ms750478(v=vs.110).aspx 的经典 WPF 桌面应用程序中使用。

You could navigate between Pageclass instances but not between Windowclass instances.

您可以在Page类实例之间导航,但不能在Window类实例之间导航。

You have to show and hide it manually using such techniques as shown in WPF. How hide/show main window from another window

您必须使用WPF 中所示的技术手动显示和隐藏它。如何从另一个窗口隐藏/显示主窗口