我知道这是旧的,但我已经根据我在这里和那里找到的答案开发了一个简单的助手,所以我在这里希望它可以帮助某人。享受..
using System;
using Android.App;
using Android.Content;
namespace MyApp.Helpers
{
#region Enums
public enum MessageBoxResult
{
None = 0,
OK,
Cancel,
Yes,
No
}
public enum MessageBoxButton
{
OK = 0,
OKCancel,
YesNo,
YesNoCancel
}
public enum MessageBoxButtonText
{
Ok,
Cancel,
Yes,
No
}
#endregion
public static class MessageBoxHelper
{
public static void Show(Context context, Action<bool> callback, string messageBoxText, string caption, MessageBoxButton buttonType)
{
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.SetTitle(caption);
//builder.SetIcon(Android.Resource.Drawable.IcDialogAlert);
alertBuilder.SetMessage(messageBoxText);
switch (buttonType)
{
case MessageBoxButton.OK:
alertBuilder.SetPositiveButton(MessageBoxButtonText.Ok.ToString(), (sender, e) => callback(true));
break;
case MessageBoxButton.OKCancel:
alertBuilder.SetPositiveButton(MessageBoxButtonText.Ok.ToString(), (sender, e) => callback(true));
alertBuilder.SetNegativeButton(MessageBoxButtonText.Cancel.ToString(), (sender, e) => callback(false));
break;
case MessageBoxButton.YesNo:
alertBuilder.SetPositiveButton(MessageBoxButtonText.Yes.ToString(), (sender, e) => callback(true));
alertBuilder.SetNegativeButton(MessageBoxButtonText.No.ToString(), (sender, e) => callback(false));
break;
}
alertBuilder.Show();
}
public static void Show(Context context, string messageBoxText)
{
Show(context, delegate(bool b) { }, messageBoxText, "", MessageBoxButton.OK);
}
public static void Show(Context context, string messageBoxText, string caption)
{
Show(context, delegate(bool b) { }, messageBoxText, caption, MessageBoxButton.OK);
}
}
}