Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Windows Phone 8.1 Back Key Press Event

I'm working on my Windows Phone 8.1 app. I want to show message when user press back key . I know the codes but something is going wrong. Visual Studio showing me redlines under MessageBox , MessageBoxResult.

How can i show message in Windows Phone 8.1 ? I think it has changed after WP7 OS.

Here the example of my codes.

public MainPage()
    {
        this.InitializeComponent();
        progRing.IsActive = true;
        Window.Current.SizeChanged += Current_SizeChanged;
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        this.NavigationCacheMode = NavigationCacheMode.Required;
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {

    }

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        string caption = "Stop music and exit?";
        string message = "If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
        e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);

        base.OnBackKeyPress(e);
    }
like image 673
ArdaZeytin Avatar asked Jul 09 '14 12:07

ArdaZeytin


2 Answers

Judging by the Windows.Phone.UI.Input namespace, you are targeting WinRT XAML based app and not Silverlight WP8.1 In WinRT there is no MessageBox.Show() method You have to use MessageDialog class The plus point is you can customise the name of buttons on the dialog box and also the process is now async which means it won't block your app from functioning while the message dialog is displayed

using Windows.UI.Popups;
...
MessageDialog dialogbox= new MessageDialog("Your message content", "title");
await dialogbox.ShowAsync();

Answer by JayDev is complete and correct

One Problem with JayDev answer is that there is no "override onBackKeyPress" in WinRT. This is what you have to do:

using Windows.Phone.UI.Input
...
protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        //This should be written here rather than the contructor
        HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }

void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
        //This is where all your 'override backkey' code goes
        //You can put message dialog and/or cancel the back key using e.Handled = true;
        }

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
        //remove the handler before you leave!
        HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }
like image 108
Saurabh3321 Avatar answered Sep 21 '22 15:09

Saurabh3321


You need to use MessageDialog rather than MessageBox. I.e

        MessageDialog message = new MessageDialog("my message");
        message.ShowAsync();

MessageDialog is in the Windows.UI.Popups namespace.

Specific to your scenario you could use.

protected async override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    string caption = "Stop music and exit?";
    string message = "If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";

    MessageDialog msgDialog = new MessageDialog(message, caption);

    //OK Button
    UICommand okBtn = new UICommand("OK");
    msgDialog.Commands.Add(okBtn);

    //Cancel Button
     UICommand cancelBtn = new UICommand("Cancel");
     cancelBtn.Invoked = (s) => { e.Cancel = true; };
     msgDialog.Commands.Add(cancelBtn);

       //Show message
     await msgDialog.ShowAsync();

    base.OnBackKeyPress(e);
}
like image 2
JayDev Avatar answered Sep 21 '22 15:09

JayDev