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);
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)
Not obvious, not documented. Not impressed.