Xamarin.iOS 未在警报中正确显示按钮
Xamarin.iOS not displaying buttons correctly in Alert
我没有在 Alert.Is 上获得 OK 按钮 这不是显示 OK 按钮警报的代码?
我需要改变什么?
public void Alert (List<string> listToAlert, string heading)
{
StringBuilder stringBuilder = new StringBuilder ();
for (int i = 0; i < listToAlert.Count; i++) {
stringBuilder.Append (listToAlert [i]);
stringBuilder.Append ("\r\n");
}
UIButton okayButton = new UIButton();
var okAlertController = UIAlertController.Create (heading, stringBuilder.ToString (), UIAlertControllerStyle.Alert);
okayButton.TouchUpInside += (sender, e) => {
//Add Action
okAlertController.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, null));
};
_rootVC.PresentViewController (okAlertController, true, null);
}
您正在添加操作,直到用户单击确定按钮,您应该在创建 UI 警报控制器后添加它
var okAlertController = UIAlertController.Create ("Title", "The message", UIAlertControllerStyle.Alert);
//Ads Action
okAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
// Present Alert
PresentViewController (okAlertController, true, null);
查看文档以查看更多示例https://docs.microsoft.com/en-us/xamarin/ios/user-interface/controls/alerts
这是一个可行的解决方案 - @Ricardo Romo 的上述解决方案无法编译。
问题很可能是围绕 class 包含的类型的假设。 PresentViewController 假定呼叫 class 是 ViewController。在我和许多情况下,以及我的情况下,这并不总是正确的。我是从 appdelegate 打电话来的。
public class AlertViewController
{
#region Static Methods
public static UIAlertController PresentOKAlert (string title, string description, UIViewController controller)
{
// No, inform the user that they must create a home first
UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
// Configure the alert
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (action) => { }));
// Display the alert
controller.PresentViewController (alert, true, null);
// Return created controller
return alert;
}
public static UIAlertController PresentOKCancelAlert (string title, string description, UIViewController controller, AlertOKCancelDelegate action)
{
// No, inform the user that they must create a home first
UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
// Add cancel button
alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, (actionCancel) => {
// Any action?
if (action != null) {
action (false);
}
}));
// Add ok button
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (actionOK) => {
// Any action?
if (action != null) {
action (true);
}
}));
// Display the alert
controller.PresentViewController (alert, true, null);
// Return created controller
return alert;
}
public static UIAlertController PresentTextInputAlert (string title, string description, string placeholder, string text, UIViewController controller, AlertTextInputDelegate action)
{
// No, inform the user that they must create a home first
UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
UITextField field = null;
// Add and configure text field
alert.AddTextField ((textField) => {
// Save the field
field = textField;
// Initialize field
field.Placeholder = placeholder;
field.Text = text;
field.AutocorrectionType = UITextAutocorrectionType.No;
field.KeyboardType = UIKeyboardType.Default;
field.ReturnKeyType = UIReturnKeyType.Done;
field.ClearButtonMode = UITextFieldViewMode.WhileEditing;
});
// Add cancel button
alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, (actionCancel) => {
// Any action?
if (action != null) {
action (false, "");
}
}));
// Add ok button
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (actionOK) => {
// Any action?
if (action != null && field != null) {
action (true, field.Text);
}
}));
// Display the alert
controller.PresentViewController (alert, true, null);
// Return created controller
return alert;
}
public static void Alert (List<string> listToAlert, string heading, UIViewController controller)
{
StringBuilder stringBuilder = new StringBuilder ();
for (int i = 0; i < listToAlert.Count; i++) {
stringBuilder.Append (listToAlert [i]);
stringBuilder.Append ("\r\n");
}
AlertViewController.PresentOKAlert (heading, stringBuilder.ToString (), controller);
}
#endregion
#region Delegates
public delegate void AlertOKCancelDelegate (bool OK);
public delegate void AlertTextInputDelegate (bool OK, string text);
#endregion
}
我没有在 Alert.Is 上获得 OK 按钮 这不是显示 OK 按钮警报的代码?
我需要改变什么?
public void Alert (List<string> listToAlert, string heading)
{
StringBuilder stringBuilder = new StringBuilder ();
for (int i = 0; i < listToAlert.Count; i++) {
stringBuilder.Append (listToAlert [i]);
stringBuilder.Append ("\r\n");
}
UIButton okayButton = new UIButton();
var okAlertController = UIAlertController.Create (heading, stringBuilder.ToString (), UIAlertControllerStyle.Alert);
okayButton.TouchUpInside += (sender, e) => {
//Add Action
okAlertController.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, null));
};
_rootVC.PresentViewController (okAlertController, true, null);
}
您正在添加操作,直到用户单击确定按钮,您应该在创建 UI 警报控制器后添加它
var okAlertController = UIAlertController.Create ("Title", "The message", UIAlertControllerStyle.Alert);
//Ads Action
okAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
// Present Alert
PresentViewController (okAlertController, true, null);
查看文档以查看更多示例https://docs.microsoft.com/en-us/xamarin/ios/user-interface/controls/alerts
这是一个可行的解决方案 - @Ricardo Romo 的上述解决方案无法编译。
问题很可能是围绕 class 包含的类型的假设。 PresentViewController 假定呼叫 class 是 ViewController。在我和许多情况下,以及我的情况下,这并不总是正确的。我是从 appdelegate 打电话来的。
public class AlertViewController
{
#region Static Methods
public static UIAlertController PresentOKAlert (string title, string description, UIViewController controller)
{
// No, inform the user that they must create a home first
UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
// Configure the alert
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (action) => { }));
// Display the alert
controller.PresentViewController (alert, true, null);
// Return created controller
return alert;
}
public static UIAlertController PresentOKCancelAlert (string title, string description, UIViewController controller, AlertOKCancelDelegate action)
{
// No, inform the user that they must create a home first
UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
// Add cancel button
alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, (actionCancel) => {
// Any action?
if (action != null) {
action (false);
}
}));
// Add ok button
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (actionOK) => {
// Any action?
if (action != null) {
action (true);
}
}));
// Display the alert
controller.PresentViewController (alert, true, null);
// Return created controller
return alert;
}
public static UIAlertController PresentTextInputAlert (string title, string description, string placeholder, string text, UIViewController controller, AlertTextInputDelegate action)
{
// No, inform the user that they must create a home first
UIAlertController alert = UIAlertController.Create (title, description, UIAlertControllerStyle.Alert);
UITextField field = null;
// Add and configure text field
alert.AddTextField ((textField) => {
// Save the field
field = textField;
// Initialize field
field.Placeholder = placeholder;
field.Text = text;
field.AutocorrectionType = UITextAutocorrectionType.No;
field.KeyboardType = UIKeyboardType.Default;
field.ReturnKeyType = UIReturnKeyType.Done;
field.ClearButtonMode = UITextFieldViewMode.WhileEditing;
});
// Add cancel button
alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, (actionCancel) => {
// Any action?
if (action != null) {
action (false, "");
}
}));
// Add ok button
alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, (actionOK) => {
// Any action?
if (action != null && field != null) {
action (true, field.Text);
}
}));
// Display the alert
controller.PresentViewController (alert, true, null);
// Return created controller
return alert;
}
public static void Alert (List<string> listToAlert, string heading, UIViewController controller)
{
StringBuilder stringBuilder = new StringBuilder ();
for (int i = 0; i < listToAlert.Count; i++) {
stringBuilder.Append (listToAlert [i]);
stringBuilder.Append ("\r\n");
}
AlertViewController.PresentOKAlert (heading, stringBuilder.ToString (), controller);
}
#endregion
#region Delegates
public delegate void AlertOKCancelDelegate (bool OK);
public delegate void AlertTextInputDelegate (bool OK, string text);
#endregion
}