当PhoneAppplicationPage(例如MainPage.xaml)退出时,如何处理事件?
我尝试处理Unloaded事件,但是当我退出页面时不会调用它。
答案 0 :(得分:6)
我认为你的意思是这个事件:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
// write exit logic
}
无论是通过按后退按钮还是主页按钮,只要您离开页面,就会调用此事件。只需将上面的内容粘贴到您网页后面的代码中,然后根据您的需要进行调整。
答案 1 :(得分:2)
出口是什么意思?当用户通过订阅PhoneApplicationPage.BackKeyPress按下后退键时,您可以处理该事件。
示例:
private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult messageBoxResult = MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel);
if (messageBoxResult != MessageBoxResult.OK)
e.Cancel = true;
}
然而,当用户通过按下主页按钮,搜索按钮,Toast通知,来电或类似信息退出应用程序时,它被称为墓碑。您可以在应用程序上处理Deactivated事件以在应用程序中保存状态,以便您可以在下次启动应用程序时恢复用户离开的位置。但是你不能“停止”墓碑 - 这样用户就无法退出应用程序。
在这里阅读更多关于墓碑的信息:
Architecting WP7 - Part 5 of 10: Tombstoning by Shawn Wildermuth
答案 2 :(得分:0)
当PhoneAppplicationPage(MainPage.xaml)退出时,
这就像关闭事件,
MainPage_PointerExited
活动适用于wp8.1
。
即使这个帖子已经4年了,我也想回答 可能对寻找答案的其他人有帮助。
Private Sub MainPage_PointerExited(sender As Object, e As PointerRoutedEventArgs) Handles Me.PointerExited
Application.Current.Exit()
'your code goes here
end sub