-
-
Save noppolp/033f011a870e8e269d87 to your computer and use it in GitHub Desktop.
MessageBox.Show for Xamarin.iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using UIKit; | |
using Foundation; | |
using System.Collections.Generic; | |
namespace UIKit | |
{ | |
public enum MessageBoxResult | |
{ | |
None = 0, | |
OK, | |
Cancel, | |
Yes, | |
No | |
} | |
public enum MessageBoxButtons | |
{ | |
OK = 0, | |
OKCancel, | |
YesNo, | |
YesNoCancel | |
} | |
public static class MessageBox | |
{ | |
public static MessageBoxResult Show(string text, string caption, MessageBoxButtons buttons) | |
{ | |
MessageBoxResult result = MessageBoxResult.Cancel; | |
bool IsDisplayed = false; | |
int buttonClicked = -1; | |
MessageBoxButtons button = buttons; | |
UIAlertView alert = null; | |
var cancelButton = "Cancel"; | |
string[] otherButtons = null; | |
switch (button) | |
{ | |
case MessageBoxButtons.OK: | |
cancelButton = string.Empty; | |
otherButtons = new [] { "OK" }; | |
break; | |
case MessageBoxButtons.OKCancel: | |
otherButtons = new [] { "OK" }; | |
break; | |
case MessageBoxButtons.YesNo: | |
cancelButton = string.Empty; | |
otherButtons = new [] { "Yes", "No" }; | |
break; | |
case MessageBoxButtons.YesNoCancel: | |
otherButtons = new [] { "Yes", "No" }; | |
break; | |
} | |
if (cancelButton != string.Empty) | |
{ | |
alert = new UIAlertView(caption, text, null, cancelButton, otherButtons); | |
} | |
else | |
{ | |
alert = new UIAlertView(caption, text, null, null, otherButtons); | |
} | |
alert.Canceled += (sender, e) => { | |
buttonClicked = 0; | |
IsDisplayed = false; | |
}; | |
alert.Clicked += (sender, e) => { | |
buttonClicked = (int)e.ButtonIndex; | |
IsDisplayed = false; | |
}; | |
alert.Dismissed += (sender, e) => { | |
if (IsDisplayed) | |
{ | |
buttonClicked = (int)e.ButtonIndex; | |
IsDisplayed = false; | |
} | |
}; | |
alert.Show(); | |
IsDisplayed = true; | |
while (IsDisplayed) | |
{ | |
NSRunLoop.Current.RunUntil(NSDate.FromTimeIntervalSinceNow(0.2)); | |
} | |
switch (button) | |
{ | |
case MessageBoxButtons.OK: | |
result = MessageBoxResult.OK; | |
break; | |
case MessageBoxButtons.OKCancel: | |
if (buttonClicked == 1) | |
result = MessageBoxResult.OK; | |
break; | |
case MessageBoxButtons.YesNo: | |
if (buttonClicked == 0) | |
result = MessageBoxResult.Yes; | |
else | |
result = MessageBoxResult.No; | |
break; | |
case MessageBoxButtons.YesNoCancel: | |
if (buttonClicked == 1) | |
result = MessageBoxResult.Yes; | |
else if (buttonClicked == 2) | |
result = MessageBoxResult.No; | |
break; | |
} | |
return result; | |
} | |
public static MessageBoxResult Show(string text) | |
{ | |
return Show(text, string.Empty, MessageBoxButtons.OK); | |
} | |
public static MessageBoxResult Show(string text, string caption) | |
{ | |
return Show(text, caption, MessageBoxButtons.OK); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment