Sca*_*axx 16 c# iphone xamarin.ios ipad
我有一个带有两个按钮的UIAlertView的是/否对话框.我希望在我的方法中实现类似于此的逻辑:
if(messagebox.Show() == DialogResult.OK)
Run Code Online (Sandbox Code Playgroud)
问题是,如果我调用UIAlertView.Show(),该过程将继续.但我需要等待用户交互的结果,并在单击第二个按钮时返回true或false depanding.MonoTouch有可能吗?
mig*_*aza 18
要做到这一点,你可以做的是手动运行mainloop.我没有设法直接停止主循环,所以我改为运行主循环0.5秒并等待用户响应.
以下函数显示了如何使用上述方法实现模式查询:
int WaitForClick ()
{
int clicked = -1;
var x = new UIAlertView ("Title", "Message", null, "Cancel", "OK", "Perhaps");
x.Show ();
bool done = false;
x.Clicked += (sender, buttonArgs) => {
Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex);
clicked = buttonArgs.ButtonIndex;
};
while (clicked == -1){
NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5));
Console.WriteLine ("Waiting for another 0.5 seconds");
}
Console.WriteLine ("The user clicked {0}", clicked);
return clicked;
}
Run Code Online (Sandbox Code Playgroud)
小智 17
基于Miguel的编码,这里是标准MessageBox的方便替代:
using System;
using System.Drawing;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Collections.Generic;
namespace YourNameSpace
{
public enum MessageBoxResult
{
None = 0,
OK,
Cancel,
Yes,
No
}
public enum MessageBoxButton
{
OK = 0,
OKCancel,
YesNo,
YesNoCancel
}
public static class MessageBox
{
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton buttonType)
{
MessageBoxResult res = MessageBoxResult.Cancel;
bool IsDisplayed = false;
int buttonClicked = -1;
MessageBoxButton button = buttonType;
UIAlertView alert = null;
string cancelButton = "Cancel";
string[] otherButtons = null;
switch (button)
{
case MessageBoxButton.OK:
cancelButton = "";
otherButtons = new string[1];
otherButtons[0] = "OK";
break;
case MessageBoxButton.OKCancel:
otherButtons = new string[1];
otherButtons[0] = "OK";
break;
case MessageBoxButton.YesNo:
cancelButton = "";
otherButtons = new string[2];
otherButtons[0] = "Yes";
otherButtons[1] = "No";
break;
case MessageBoxButton.YesNoCancel:
otherButtons = new string[2];
otherButtons[0] = "Yes";
otherButtons[1] = "No";
break;
}
if (cancelButton.Length > 0)
alert = new UIAlertView(caption, messageBoxText, null, cancelButton, otherButtons);
else
alert = new UIAlertView(caption, messageBoxText, null, null, otherButtons);
alert.BackgroundColor = UIColor.FromWhiteAlpha(0f, 0.8f);
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;
while (IsDisplayed)
{
NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.2));
}
switch (button)
{
case MessageBoxButton.OK:
res = MessageBoxResult.OK;
break;
case MessageBoxButton.OKCancel:
if (buttonClicked == 1)
res = MessageBoxResult.OK;
break;
case MessageBoxButton.YesNo:
if (buttonClicked == 0)
res = MessageBoxResult.Yes;
else
res = MessageBoxResult.No;
break;
case MessageBoxButton.YesNoCancel:
if (buttonClicked == 1)
res = MessageBoxResult.Yes;
else if (buttonClicked == 2)
res = MessageBoxResult.No;
break;
}
return res;
}
public static MessageBoxResult Show(string messageBoxText)
{
return Show(messageBoxText, "", MessageBoxButton.OK);
}
public static MessageBoxResult Show(string messageBoxText, string caption)
{
return Show(messageBoxText, caption, MessageBoxButton.OK);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10752 次 |
| 最近记录: |