Windows Phone MessageBox WTF

So, everybody’s used the messagebox in Microsoft platforms in one form or another. The good ol’ fashioned Win32 API version, or any of the .NET variants. So, I recently got bit in the ass by Windows Phone. Here’s how.

This was what I was doing:

      MessageBox.Show(“Something went wrong…”, “Oopsie!”, MessageBoxButton.OK);

 

      if (NavigationService.CanGoBack)

      {

            NavigationService.GoBack();

      }

 

 

 

Basically, when something went wrong, display a messagebox, and when it is closed, by the [OK] button being pressed (which, I thought, was the only way it could be closed, because there is no other button including a close [X] button), the app navigated back. So I get a crash report. And what could go wrong?

 

Here’s what went wrong. Apparently, if the messagebox (modal, mind you) is being shown, and you switch away from the app, like by pressing the Start button, MessageBox.Show() returns. What? Since when? If you look at it closely, when the app navigates away, you can actually see the messagebox being closed. So, the code above crashes because you cannot navigate when your app is not active.

 

So, I looked through the documentation in MSDN, and did not notice any remark about this behaviour at all. I think it is more than reasonable to expect that the messagebox will still be there where you ‘go back’ to your app. Apparently, some people didn’t think so. What’s the rationale in that?

 

Oh, and when the Show() method magically returns, the result is MessageBoxResult.None. Therefore, I’d have to actually check the return result of the call like so:

 

      MessageBoxResult result = MessageBox.Show(“Something went wrong…”, “Oopsie!”, MessageBoxButton.OK);

 

      if (result == MessageBoxResult.OK)

      {

            if (NavigationService.CanGoBack)

            {

                  NavigationService.GoBack();

            }

      }

 

 

Not obvious, not documented. Not impressed.

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Windows Phone MessageBox WTF

  1. me says:

    c sharp minor buddy. you had it all wrong

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