简体   繁体   English

在 WPF 中的页面之间导航?

[英]Navigation between pages in WPF?

I have my WPF application and my button is on a WINDOW that I added and I want the button to open a PAGE when I click it.我有我的 WPF 应用程序,我的按钮位于我添加的 WINDOW 上,我希望该按钮在我单击时打开一个 PAGE。

NavigationService nav = NavigationService.GetNavigationService(this); 
nav.Navigate(new Uri("xamlFeedbackPage.xaml", UriKind.RelativeOrAbsolute));

I have tried that code that was online and my application crashes when I click the button.我试过那个在线的代码,当我点击按钮时,我的应用程序崩溃了。

Any help?有什么帮助吗?

Take a look at this post and this MSDN article .看看这篇文章和这篇MSDN 文章 They contain explanation about what kind of Types are suitable for navigation (pages) and in which container to host them (basically a Frame).它们包含有关哪种类型适合导航(页面)以及在哪个容器中托管它们(基本上是框架)的说明。 Then you should have some succes.那么你应该有一些成功。

EDIT编辑

Take a look at this extensive example and things will become clear.看看这个广泛的例子,事情就会变得清晰起来。

    public PageFunction1()
    {
        InitializeComponent();
    }

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

No need of call anything in the code.Since it can be done with xaml itself.不需要在代码中调用任何东西。因为它可以用 xaml 本身来完成。

App.xaml应用程序.xaml

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

Page1第1页

        <Page x:Class="WpfApplication1.Page1"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="Page1">
            <StackPanel>
                <TextBlock>
                    Go to <Hyperlink NavigateUri="Page2.xaml"> Page 2 </Hyperlink>
                </TextBlock>
            </StackPanel>
        </Page>

Navigate sample导航示例

Page 2第 2 页

        <Page x:Class="WpfApplication1.Page2"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="Page2"> 
            <StackPanel>
                <TextBlock Margin="10">Welcome to Page2.</TextBlock>
                <TextBlock Margin="10">
                    Go back to <Hyperlink NavigateUri="Page1.xaml"> Page 1 </Hyperlink>
                </TextBlock>
            </StackPanel>
        </Page>

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

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