Skip to content

Instantly share code, notes, and snippets.

@CH3COOH
Last active December 16, 2015 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CH3COOH/5449515 to your computer and use it in GitHub Desktop.
Save CH3COOH/5449515 to your computer and use it in GitHub Desktop.
MessageBox.Show for Xamarin.iOS description: http://blog.ch3cooh.jp/entry/20130425/1366875327
using System;
using System.Drawing;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Collections.Generic;
namespace MonoTouch.UIKit
{
public enum MessageBoxResult
{
None = 0,
OK,
Cancel,
Yes,
No
}
public enum MessageBoxButtons
{
OK = 0,
OKCancel,
YesNo,
YesNoCancel
}
/// <summary>
/// メッセージ ボックスを表示します。メッセージ ボックスには、テキスト、ボタンを格納できます。
/// </summary>
public static class MessageBox
{
/// <summary>
/// 指定したテキスト、キャプション、およびボタンを表示するメッセージ ボックスを表示します。
/// </summary>
/// <param name="text">メッセージ ボックスに表示するテキスト。</param>
/// <param name="caption">メッセージ ボックスのタイトル バーに表示するテキスト。</param>
/// <param name="buttons">メッセージ ボックスに表示されるボタンを指定する MessageBoxButtons 値の 1 つ。</param>
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";
var 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 = e.ButtonIndex;
IsDisplayed = false;
};
alert.Dismissed += (sender, e) => {
if (IsDisplayed)
{
buttonClicked = e.ButtonIndex;
IsDisplayed = false;
}
};
alert.Show();
IsDisplayed = true; // このフラグがfalseになるとループから抜ける
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;
}
/// <summary>
/// 指定したテキストを表示するメッセージ ボックスを表示します。
/// </summary>
/// <param name="text">メッセージ ボックスに表示するテキスト。</param>
public static MessageBoxResult Show(string text)
{
return Show(text, string.Empty, MessageBoxButtons.OK);
}
/// <summary>
/// 指定したテキストとキャプションを表示するメッセージ ボックスを表示します。
/// </summary>
/// <param name="text">メッセージ ボックスに表示するテキスト。</param>
/// <param name="caption">メッセージ ボックスのタイトル バーに表示するテキスト。</param>
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