//from www.danielmoth.com/Blog/ using System; using System.Threading.Tasks; public enum MyMessageBoxResult { Button1, Button2, None } public static class MyMessageBox { public static async Task Show(string messageBoxText) { return await MyMessageBox.Show(messageBoxText, " "); } public static async Task Show(string messageBoxText, string caption) { return await MyMessageBox.Show(messageBoxText, caption, TranslatorByMoth.AppResources.MyMessageBox_button1_OK); } public static async Task Show(string messageBoxText, string caption, string button1) { return await MyMessageBox.Show(messageBoxText, caption, button1, null); } /// /// Displays a message box that contains the specified text, title bar caption, and two response buttons. /// /// The message to display. /// The title of the message box. /// The title of the first button. Default. /// The title of the second button. Pass null if you only want one button. /// A value that indicates the user's response to the message. #if NETFX_CORE public static async Task Show(string messageBoxText, string caption, string button1, string button2) { var result = MyMessageBoxResult.None; var msgbox = new Windows.UI.Popups.MessageDialog(messageBoxText, caption); if (button1 != null) //TODO omit these and then file the bug on debugger (stack trace the only clue) { msgbox.Commands.Add(new Windows.UI.Popups.UICommand(button1, _ => { result = MyMessageBoxResult.Button1; })); if (button2 != null) msgbox.Commands.Add(new Windows.UI.Popups.UICommand(button2, _ => { result = MyMessageBoxResult.Button2; })); } await msgbox.ShowAsync(); return result; } #else public static async Task Show(string messageBoxText, string caption, string button1, string button2) { // don't use MyDebug here, *if* MyDebug uses MyMesageBox, to prevent infinite loop System.Diagnostics.MyDebug.Assert(messageBoxText.Length < 256 && caption.Length < 256); try { // fixes crashes with v1.2 when no connection msg and tries to translate at same time so overlapping msg tries to display if (Microsoft.Xna.Framework.GamerServices.Guide.IsVisible) return MyMessageBoxResult.None; int? returned = await Task.Factory.FromAsync( (callback, state) => Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox( caption, messageBoxText, (button2 == null) ? new string[] { button1 } : new string[] { button1, button2 }, 0, // can choose which button has the focus Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.None, // can play sounds callback, state ), Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox, null); if (!returned.HasValue) return MyMessageBoxResult.None; else if (returned == 0) return MyMessageBoxResult.Button1; else if (returned == 1) return MyMessageBoxResult.Button2; else return MyMessageBoxResult.None; } catch (Microsoft.Xna.Framework.GamerServices.GuideAlreadyVisibleException) { // same as if statement above to fix v1.2 crash // fallback due to crash reports on Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox offset 96 and 184 System.Diagnostics.MyDebug.Assert(Microsoft.Xna.Framework.GamerServices.Guide.IsVisible == true); return MyMessageBoxResult.None; } catch (System.ArgumentException e) { // > 256 chars System.Diagnostics.MyDebug.Assert(false, e.Message); System.Windows.MessageBox.Show(messageBoxText, caption, System.Windows.MessageBoxButton.OK); return MyMessageBoxResult.None; } catch (System.Exception e) { // Should never come here, just being paranoid due to the aforementioned v1.2 crash System.Diagnostics.MyDebug.Assert(false, e.Message); System.Windows.MessageBox.Show(messageBoxText, caption, System.Windows.MessageBoxButton.OK); return MyMessageBoxResult.None; } } #endif }