gpt4 book ai didi

c# - Windows 手机 : How to stay in the Current page rather than navigating to another page?

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:54 27 4
gpt4 key购买 nike

我有两个页面(MainPage 和 page1)。当用户在 page1 时,如果用户按下后退键,将弹出以下消息:“您确定要退出吗?”

因此,如果用户按 OK,那么它应该导航到另一个页面,如果用户按 Cancel,它应该停留在同一页面。这是我的代码:

此代码是在 Page1.Xaml 中编写的:

Protected override void OnBackKeyPrss(System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult res = MessageBox.show("Are you sure that you want to exit?",
"", MessageBoxButton.OkCancel);
if(res==MessageBoxResult.OK)
{
App.Navigate("/mainpage.xaml");
}
else
{
//enter code here
}

}

但是,当我按下取消时,它仍然导航到 mainpage.xaml。我该如何解决这个问题?

最佳答案

使用 e.Cancel = true; 取消返回导航。

如有错误请指正。您的代码看起来一团糟。我认为您的最后一页/后页是 mainpage.xaml 并且在 OK 中您将再次导航到此页面。如果是这种情况,则无需再次导航,您可以使用以下代码。

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult res = MessageBox.Show("Are you sure that you want to exit?",
"", MessageBoxButton.OKCancel);
if (res != MessageBoxResult.OK)
{
e.Cancel = true; //when pressed cancel don't go back
}
}

关于c# - Windows 手机 : How to stay in the Current page rather than navigating to another page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18094012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com