如何在WPF中使用按钮导航到其他页面 [英] How to navigate to other page with button in WPF

查看:167
本文介绍了如何在WPF中使用按钮导航到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Page2.xaml 的名称下设置了第二个.xaml页面,我想将其设置为点击后,用户被带到 Page2.xaml

I have a second .xaml page set up under the name Page2.xaml and I want to make it so that when my button is clicked, the user is taken to Page2.xaml

我的 Page1.xaml 中的按钮:

I have this for my button inside of my Page1.xaml:

<Grid>
    <Button x:Name="localModeBtn" 
            Style="{StaticResource MainButtonStyle}"  
            Content="local mode" 
            Click="localModeBtn_Click" />
</Grid>

对于按钮事件处理程序:

And for the button event handler:

private void localModeBtn_Click(object sender, RoutedEventArgs e)
    {
        Uri uri = new Uri("Page2.xaml", UriKind.Relative);
        this.NavigationService.Navigate(uri);
    }

单击按钮后,我收到一条错误消息,内容为无法找到资源 page2.xaml
事情是 Page2.xaml Pag1.xaml 处于同一文件夹中,所以我看不到哪里出了错?

Upon clicking the button I receive an error that says "Cannot locate resource page2.xaml" The thing is that Page2.xaml is in the same folder as Pag1.xaml so I can't see where I've gone wrong?

推荐答案

我自己的问题的解决方案:

Solution to my own question:

我觉得提供解决方案有点傻我自己的问题,但要感谢Jasti的链接我能够整理我的代码。由于他只发表了评论,所以我不能将其标记为答案,所以这里是解决方法。

I feel a bit silly providing a solution to my own question but thanks to Jasti's link I was able to sort my code out. As he had only posted a comment, I can't mark it as an answer, so here is the solution.

我将NavigationWindow更改为Window并插入:

I changed the NavigationWindow on to a Window and inserted:

<DockPanel>
    <Frame x:Name="_NavigationFrame" NavigationUIVisibility="Hidden" />
</DockPanel>

在MainWindow.xaml.cs的构造函数中,我添加了:

And within the constructor of the MainWindow.xaml.cs I added:

_NavigationFrame.Navigate(new Page1());

然后,最后一步是将按钮事件处理程序调整为:

Then the last step was to adjust the button event handler to:

this.NavigationService.Navigate(new Uri("Pages/Page2.xaml", UriKind.Relative));

这篇关于如何在WPF中使用按钮导航到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆