在WPF中的页面之间导航?

时间:2013-11-18 15:27:31

标签: c# wpf xaml window

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

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

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

任何帮助?

3 个答案:

答案 0 :(得分: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));
    }

答案 1 :(得分:1)

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

修改

看看this extensive example,事情会变得清晰。

答案 2 :(得分:0)

不需要在代码中调用任何东西,因为它可以通过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>
相关问题