添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

本文介绍如何使用 UIAlertController 向 Xamarin.tvOS 用户显示警报消息。

如果需要引起 tvOS 用户的注意或请求执行破坏性操作 ((例如删除文件) ),可以使用以下命令 UIAlertViewController 显示警报消息:

如果除了显示消息外,还可以向警报添加按钮和文本字段,以允许用户响应操作并提供反馈。

如上所述,警报用于引起用户的注意,并告知他们应用的状态或请求反馈。 警报必须显示标题,他们可以选择有一条消息和一个或多个按钮或文本字段。

Apple 提供以下有关使用警报的建议:

  • 以稀疏方式使用警报 - 警报会中断用户的流并中断用户体验,因此,仅应用于错误通知、In-App购买和破坏性操作等重要情况。
  • 提供有用的选项 - 如果警报向用户提供选项,则应确保每个选项提供关键信息,并为用户执行有用的操作。
  • 警报标题和消息

    Apple 提供了以下建议来演示警报的标题和可选消息:

  • 使用多字游戏 - 警报的游戏应清楚地了解情况的要点,同时仍保持简单。 单个单词标题很少提供足够的信息。
  • 使用不需要消息的描述性标题 - 尽可能考虑使警报的标题具有足够的描述性,以便不需要可选消息文本。
  • 使消息成为简短的完整句子 - 如果需要可选的消息来获取警报的点,请尽可能简单,并使它成为具有适当大写和标点符号的完整句子。
  • Apple 有以下将按钮添加到警报的建议:

  • 限制为两个按钮 - 尽可能将警报限制为最多两个按钮。 单按钮警报提供信息,但没有操作。 两个按钮警报提供简单的是/无选择的操作。
  • 使用简洁的逻辑按钮标题 - 简单一到两个单词按钮标题,清楚地描述按钮的操作效果最佳。 有关详细信息,请参阅“ 使用按钮” 文档。
  • 清楚地标记破坏性按钮 - 对于执行破坏性操作的按钮 (,例如删除文件) 清楚地用 UIAlertActionStyle.Destructive 样式标记它们。
  • 若要显示警报,请通过添加操作 (按钮) 并选择警报样式来创建 UIAlertViewController 实例并对其进行配置。 例如,以下代码显示“确定/取消”警报:

    const string title = "A Short Title is Best";
    const string message = "A message should be a short, complete sentence.";
    const string acceptButtonTitle = "OK";
    const string cancelButtonTitle = "Cancel";
    const string deleteButtonTitle = "Delete";
    var alertController = UIAlertController.Create (title, message, UIAlertControllerStyle.Alert);
    // Create the action.
    var acceptAction = UIAlertAction.Create (acceptButtonTitle, UIAlertActionStyle.Default, _ =>
        Console.WriteLine ("The \"OK/Cancel\" alert's other action occurred.")
    var cancelAction = UIAlertAction.Create (cancelButtonTitle, UIAlertActionStyle.Cancel, _ =>
        Console.WriteLine ("The \"OK/Cancel\" alert's other action occurred.")
    // Add the actions.
    alertController.AddAction (acceptAction);
    alertController.AddAction (cancelAction);
    PresentViewController (alertController, true, null);
    

    让我们详细查看此代码。 首先,我们将创建一个新的警报,其中包含给定的标题和消息:

    UIAlertController.Create (title, message, UIAlertControllerStyle.Alert)
    

    接下来,对于要在警报中显示的每个按钮,我们将创建一个操作,用于定义按钮标题、其样式以及按下按钮时要执行的操作:

    UIAlertAction.Create ("Button Title", UIAlertActionStyle.Default, _ =>
        // Do something when the button is pressed
    

    枚举 UIAlertActionStyle 允许将按钮的样式设置为以下选项之一:

  • 默认值 - 按钮将是显示警报时选择的默认按钮。
  • 取消 - 按钮是警报的取消按钮。
  • 破坏性 - 突出显示按钮作为破坏性操作,例如删除文件。 目前,tvOS 呈现具有红色背景的“破坏性”按钮。
  • 该方法 AddAction 将给定操作添加到 UIAlertViewController 给定操作,最后 PresentViewController (alertController, true, null) 该方法向用户显示给定警报。

    添加文本字段

    除了向警报添加操作 (按钮) 之外,还可以将文本字段添加到警报,以允许用户填写用户 ID 和密码等信息:

    如果用户选择“文本字段”,将显示标准 tvOS 键盘,允许他们输入字段的值:

    以下代码显示一个“确定/取消警报”,其中包含一个用于输入值的文本字段:

    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) => {
        // User canceled, do something
    // Add ok button
    alert.AddAction(UIAlertAction.Create("OK",UIAlertActionStyle.Default,(actionOK) => {
        // User selected ok, do something
    // Display the alert
    controller.PresentViewController(alert,true,null);
    

    该方法 AddTextField 向警报添加新的文本字段,然后可以通过设置属性(如占位符文本 (字段为空时显示的文本) 、默认文本值和键盘类型)进行配置。 例如:

    // Initialize field
    field.Placeholder = placeholder;
    field.Text = text;
    field.AutocorrectionType = UITextAutocorrectionType.No;
    field.KeyboardType = UIKeyboardType.Default;
    field.ReturnKeyType = UIReturnKeyType.Done;
    field.ClearButtonMode = UITextFieldViewMode.WhileEditing;
    

    因此,我们可以稍后对文本字段的值执行操作,我们还保存使用以下代码的副本:

    UITextField field = null;
    // Add and configure text field
    alert.AddTextField ((textField) => {
        // Save the field
        field = textField;
    

    用户输入文本字段中的值后,可以使用 field 变量来访问该值。

    警报视图控制器帮助程序类

    由于使用 UIAlertViewController 警报的简单常见类型可能会导致大量重复代码,因此可以使用帮助程序类来减少重复代码的数量。 例如:

    using System;
    using Foundation;
    using UIKit;
    using System.CodeDom.Compiler;
    namespace UIKit
        /// <summary>
        /// Alert view controller is a reusable helper class that makes working with <c>UIAlertViewController</c> alerts
        /// easier in a tvOS app.
        /// </summary>
        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 PresentDestructiveAlert(string title, string description, string destructiveAction, 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(destructiveAction,UIAlertActionStyle.Destructive,(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;
            #endregion
            #region Delegates
            public delegate void AlertOKCancelDelegate(bool OK);
            public delegate void AlertTextInputDelegate(bool OK, string text);
            #endregion
    

    使用此类,可以按如下所示显示和响应简单的警报:

    #region Custom Actions
    partial void DisplayDestructiveAlert (Foundation.NSObject sender) {
        // User helper class to present alert
        AlertViewController.PresentDestructiveAlert("A Short Title is Best","The message should be a short, complete sentence.","Delete",this, (ok) => {
            Console.WriteLine("Destructive Alert: The user selected {0}",ok);
    partial void DisplayOkCancelAlert (Foundation.NSObject sender) {
        // User helper class to present alert
        AlertViewController.PresentOKCancelAlert("A Short Title is Best","The message should be a short, complete sentence.",this, (ok) => {
            Console.WriteLine("OK/Cancel Alert: The user selected {0}",ok);
    partial void DisplaySimpleAlert (Foundation.NSObject sender) {
        // User helper class to present alert
        AlertViewController.PresentOKAlert("A Short Title is Best","The message should be a short, complete sentence.",this);
    partial void DisplayTextInputAlert (Foundation.NSObject sender) {
        // User helper class to present alert
        AlertViewController.PresentTextInputAlert("A Short Title is Best","The message should be a short, complete sentence.","placeholder", "", this, (ok, text) => {
            Console.WriteLine("Text Input Alert: The user selected {0} and entered `{1}`",ok,text);
    #endregion
    

    本文介绍如何 UIAlertController 在 Xamarin.tvOS 中向用户显示警报消息。 首先,它演示了如何显示简单的警报并添加按钮。 接下来,它演示了如何将文本字段添加到警报。 最后,它演示了如何使用帮助程序类来减少显示警报所需的重复代码量。

  • tvOS 示例
  • tvOS 人机界面指南
  • tvOS 应用编程指南
  •