Care.MainPage.OnBackKeyPress C# (CSharp) Method

OnBackKeyPress() protected method

protected OnBackKeyPress ( System e ) : void
e System
return void
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("确定退出程序?", "提示", MessageBoxButton.OKCancel);
            // 如果确实退出, 则清空back stack,退出
            if (result == MessageBoxResult.OK)
            {
                while (NavigationService.BackStack.Any())
                {
                    NavigationService.RemoveBackEntry();
                }
                // 因为这里本质上是通过抛异常退出的,所以应该提前收尾
                // 比如保存本地存储
                PreferenceHelper.SavePreference();
                NavigationService.GoBack();
            }
            // 否则取消退出过程
            e.Cancel = true;
        }