【问题标题】:Navigation between pages in WPF?WPF中页面之间的导航?
【发布时间】:2013-11-18 15:27:31
【问题描述】:

我有我的 WPF 应用程序,我的按钮位于我添加的 WINDOW 上,我希望按钮在单击时打开一个 PAGE。

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

我已经尝试过在线的代码,当我单击按钮时我的应用程序崩溃了。

有什么帮助吗?

【问题讨论】:

  • “xamlFeedbackPage.xaml”是否位于项目的根目录中?否则,您可能必须将其更改为“/Pages/xamlFeedbackPage.xaml”之类的内容。并尝试将 UriKind 设置为 Absolute。
  • @YoupTube i.stack.imgur.com/i2R5G.png 是的,从图片中可以看出,它是包含所有其他页面/窗口的根文件夹。
  • 我看到 UriKind 并将其更改为绝对值?
  • 导航服务是否存在有效(非空)引用?
  • 我认为@YoupTube 的意思是让您确保nav 不为空。

标签: c# wpf xaml window


【解决方案1】:

看看this post 和这个MSDN article。它们包含关于哪种类型适合导航(页面)以及在哪个容器中托管它们(基本上是框架)的说明。那么你应该有一些成功。

编辑

看看this extensive example,事情就清楚了。

【讨论】:

  • 嘿,看看他们的文章,我想试着关注他们,尽管我认为这是因为你只能从一个页面导航到另一个页面,我试图从一个窗口转到一个页面。跨度>
  • 我创建了一个名为 HostInWindowFrame.xaml 的新窗口并添加了一个框架,但我不明白它的工作方式?
  • 然后您将 Frame.Content 设置为特定页面(上面有一个按钮),当您按下按钮时,您会调用您最初发布的代码。
【解决方案2】:
    public PageFunction1()
    {
        InitializeComponent();
    }

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

【讨论】:

  • 你只是使用页面而不是使用窗口或者你可以使用导航窗口..你得到我了吗?
  • 请在您的回答中添加描述
【解决方案3】:

不需要在代码中调用任何东西。因为它可以用 xaml 本身来完成。

App.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>

第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

第 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>

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多