I have made a custom messagebox (discussed here) that shows localized quit prompt.
protected override void OnBackKeyPress(CancelEventArgs e)
{
//some conditions
e.Cancel = true;
string quitText = DeviceWrapper.Localize("QUIT_TEXT");
string quitCaption = DeviceWrapper.Localize("QUIT_CAPTION");
string quitOk = DeviceWrapper.Localize("DISMISS");
string quitCancel = DeviceWrapper.Localize("MESSAGEBOX_CANCEL");
IAsyncResult asyncResult = Guide.BeginShowMessageBox(
quitCaption, quitText, new List<string> { quitOk, quitCancel },
0, MessageBoxIcon.Error, null, null);
asyncResult.AsyncWaitHandle.WaitOne();
int? result = Guide.EndShowMessageBox(asyncResult);
if (result.HasValue && result.Value == 0)
e.Cancel = false;
//some more features
}
It works fine, but it does crash when it's run as stand-alone (without Visual Studio) after few seconds if user doesn't press anything.
I tried reproducing the same crash with phone attached using Release and Debug from Visual Studion, but it's as stable as it can possibly be. It only crashes when app is run from the phone itself.
How do I find the issue? What could be the reason for it? As far as I can tell, I cannot access crash logs on the device. There should be something with the messagebox logic perhaps?
UPD Few notes on your suggestion, Neil:
first of all VS warns me "the async method lacks await" stuff.
secondly I am not sure how to return info to "BackKeyPressed" that e.Cancel should equal "false". This should be the safe way to quit an app AFAIK.
And wouldn't we quit the "OnBackKeyPress" method if we run async method from it? That would mean that we can't let it know about our decision (e.Cancel = false).
Or if we don't quit the "OnBackKeyPress" then it could mean this event will stay for too long and the whole purpose is lost - app will be killed, right?
UPD2:
It seems that I am between Scylla and Charybdis: either I show a messagebox and experience crashes in runtime which is discouraged by guidelines, or I don't show a messagebox at all.
I've tried both native MessageBox and my implementation of custom messagebox with localized "Ok" and "Cancel" buttons. They behave the same: either they are synchronous and hang OnBackKeyPress
, or they are async and OnBackKeyPress
exits before we can let it know about user's decision.
Final decision
Apparently the guidelines state that we shouldn't ask user's confirmation at all. Since there is no viable way to implement a working quit confirmation messagebox without crashing I've decided to not show it at all.