Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

c# - how to override windows back button in windows phone 8 ?

I need to ovveride the windows back button when i am clicking from my application .I tried 2 method below but it is not working in windows phone 8 ?

methods 1)

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

            {

           e.Cancel = true;

           var result = MessageBox.Show("Do you want to exit?", "Attention!",
                                      MessageBoxButton.OKCancel);

        if (result == MessageBoxResult.OK)
        {
           // base.OnBackKeyPress(e);
            return;
        }
        e.Cancel = true;
    }

method 2 )

a) wrote this in xaml heading

BackKeyPress="MyBackKeyPress"

b) Initlize this in constructor

 BackKeyPress += OnBackKeyPress;

c) and call this method

   private void MyBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true; //tell system you have handled it
        //you c
    }

the two of the methods are not working . when i was clicked the back button the app destroyed and unable to get the control of backbutton . Any help ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per certification requirements it is not recommended to override the back button -

Pressing the Back button from the first screen of an app must close the app.

http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh184840%28v=vs.105%29.aspx

You can try this code

protected override void OnBackKeyPress(CancelEventArgs e)
{
    if(MessageBox.Show("Are you sure you want to exit?","Confirm Exit?", 
                            MessageBoxButton.OKCancel) != MessageBoxResult.OK)
    {
        e.Cancel = true; 

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...