Metro Nuggets

Bitesized tidbits for building Modern (Metro) apps.

Using the CustomMessageBox in OnBackKeyPressed

Sometimes in your app you may need to prompt the user for something when they’ve tapped the back button on the phone. If you wanted to offer the user a little more than just Ok/Cancel buttons, then you might have thought about using the CustomMessageBox from the Windows Phone toolkit, I know I did. But I was running into a nightmare problem where the user would press back, the prompt would  should and then almost instantly close. Every blog post I could find had the code in the same way, so how to fix?

 

The Solution

First off, here’s the code that I’d been using which matched every other example I’d found:

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (!App.SettingsWrapper.AppSettings.DontShowBackExitMessage)
            {
                e.Cancel = true;

                    var message = new CustomMessageBox
                    {
                        Title = "Are you sure?",
                        Message = "You just pressed the back key which would exit you from this game, is that what you want to so?",
                        LeftButtonContent = "Yes, please",
                        RightButtonContent = "Oops, no"
                    };
                    message.Dismissed += (sender, args) =>
                    {
                        ((CustomMessageBox)sender).Dismissing += (o, eventArgs) => eventArgs.Cancel = true;
                        if (args.Result == CustomMessageBoxResult.LeftButton)
                        {
                            NavigationService.GoBack();
                        }
                    };

                    message.Show();
            }
        }

I’d been discussing this problem with @PedroLamas and he gave me the very simple suggestion of putting all the CustomMessageBox code within a Dispatcher (even though it’s on the UI thread already). And wouldn’t you know it, it fixed the problem immediately. I’m not sure what seemed to change between all the blog posts and Stackoverflow problems I found and having to use this solution, but there we are.

So the fixed code looks like this:

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (!App.SettingsWrapper.AppSettings.DontShowBackExitMessage)
            {
                e.Cancel = true;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    var message = new CustomMessageBox
                    {
                        Title = "Are you sure?",
                        Message = "You just pressed the back key which would exit you from this game, is that what you want to so?",
                        LeftButtonContent = "Yes, please",
                        RightButtonContent = "Oops, no"
                    };
                    message.Dismissed += (sender, args) =>
                    {
                        ((CustomMessageBox)sender).Dismissing += (o, eventArgs) => eventArgs.Cancel = true;
                        if (args.Result == CustomMessageBoxResult.LeftButton)
                        {
                            NavigationService.GoBack();
                        }
                    };

                    message.Show();
                });
            }
        }

NOTE: One of the other things I tried was swapping out the CustomMessageBox for the Coding4Fun Toolkit’s MessagePrompt, but this was giving me the same problem. Incidentally, the same fix applies to this as well.

SL

Advertisements

6 responses to “Using the CustomMessageBox in OnBackKeyPressed

  1. Pingback: Windows Store Developer Links – 2013-06-26 | Dan Rigby

  2. Gaulois November 10, 2013 at 16:06

    Thanks men, this just saves my life!

  3. Kevin December 17, 2013 at 15:48

    This is because the CustomMessageBox has a handler for the OnBackKeyPressed event which responds by closing itself.

  4. Fael March 25, 2014 at 21:27

    😀 thanks man !

  5. Benny May 26, 2014 at 18:59

    Thanks man, saved me a lot of headache.

  6. Pingback: Show CustomMessageBox before exit application by pressing back key - Popular Windows Phone Questions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: