windows phone7 学习笔记04——重写返回键

  如果回退到前一个页面是一个不合理的行为,比如打开了一个弹出框,用户可能会按下返回键来关闭弹出框。在这种情况下,他们的本意不是离开应用程序,“返回”仅仅意味着摆脱弹出的对话框。重写有两种方法,第一种是重写OnBackKeyPress事件,第二种是重写PhoneApplicationPage_BackKeyPress事件。

  下面是在Windows Phone中重写返回键的代码

  1、重写OnBackKeyPress事件


protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)


        {


            e.Cancel = true;


            if (MessageBox.Show("保存工程??", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)


            {


                App.Intilize();


                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));


            }


        }


  2、重写PhoneApplicationPage_BackKeyPress事件
  添加事件:

  

  重写:

  

  这些要注意的是,不能忘记了取消原来的返回键操作,即不要忘了e.Cancel = true。



原文地址:https://www.cnblogs.com/zhangkai2237/p/2349321.html