Xamarin.Mac 中的警报
本文介绍如何在 Xamarin.Mac 应用程序中使用警报。 它介绍如何通过 C# 代码创建和显示警报以及响应用户交互。
在 Xamarin.Mac 应用程序中使用 C# 和 .NET 时,可以访问与开发人员 Objective-C 和 Xcode 相同的警报。
警报是一种特殊类型的对话,在发生严重问题 ((例如错误) )或警告 ((例如准备) 删除文件)时会出现。 由于警报是对话,因此还需要用户响应才能关闭警报。
本文介绍在 Xamarin.Mac 应用程序中使用警报的基础知识。
警报简介
警报是一种特殊类型的对话,在发生严重问题 ((例如错误) )或警告 ((例如准备) 删除文件)时会出现。 由于警报会干扰用户,因为它们必须先消除,然后用户才能继续执行其任务,因此除非绝对必要,否则请避免显示警报。
Apple 建议遵循以下准则:
- 不要仅将警报用于向用户提供信息。
- 不要显示常见可撤消操作的警报。 即使这种情况可能会导致数据丢失。
- 如果某个情况值得发出警报,请避免使用任何其他 UI 元素或方法来显示它。
- 应谨慎使用“警告”图标。
- 在警报消息中清晰而简洁地描述警报情况。
- 默认按钮名称应对应于你在警报消息中描述的操作。
有关详细信息,请参阅 Apple OS X 人机接口指南的警报部分
警报剖析
如上所述,当发生严重问题时,应向应用程序的用户显示警报,或者作为潜在数据丢失 ((例如关闭未保存的文件) )的警告。 在 Xamarin.Mac 中,使用 C# 代码创建警报,例如:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Critical,
InformativeText = "We need to save the document here...",
MessageText = "Save Document",
};
alert.RunModal ();
上面的代码显示一个警报,其中应用程序图标叠加在警告图标、标题、警告消息和单个 “确定” 按钮上:
Apple 提供了多个可用于自定义警报的属性:
- AlertStyle 将警报的类型定义为以下类型之一:
- 警告 - 用于警告用户当前或即将发生的非关键事件。 这是默认样式。
- 信息性 - 用于警告用户当前或即将发生的事件。 目前,警告和信息之间没有明显的区别
- 严重 - 用于警告用户即将发生的事件 ((例如删除文件) )的严重后果。 应谨慎使用这种类型的警报。
- MessageText - 这是警报main消息或标题,应向用户快速定义情况。
- 信息文本 - 这是警报的正文,应在其中明确定义情况并向用户提供可行的选项。
- 图标 - 允许向用户显示自定义图标。
- HelpAnchor&ShowsHelp - 允许将警报绑定到应用程序 HelpBook 并显示警报的帮助。
- 按钮 - 默认情况下,警报仅包含 “确定” 按钮,但 Buttons 集合允许根据需要添加更多选项。
- ShowsSuppressionButton - 如果
true
显示一个复选框,用户可以使用该复选框来禁止触发该警报的事件的后续发生。 - AccessoryView - 允许将另一个子视图附加到警报以提供额外信息,例如为数据输入添加 文本字段 。 如果设置新的 AccessoryView 或修改现有 AccessoryView ,则需要调用
Layout()
方法来调整警报的可见布局。
显示警报
可通过两种不同的方式显示警报:Free-Floating或作为工作表显示。 以下代码将警报显示为自由浮动:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.RunModal ();
如果运行此代码,则显示以下内容:
以下代码显示与工作表相同的警报:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.BeginSheet (this);
如果运行此代码,将显示以下内容:
使用警报按钮
默认情况下,警报仅显示 “确定” 按钮。 但是,不仅限于此,可以通过将额外的按钮追加到 Buttons 集合来创建这些按钮。 以下代码使用 “确定”、“ 取消 ”和“ 可能 ”按钮创建自由浮动警报:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.AddButton ("Ok");
alert.AddButton ("Cancel");
alert.AddButton ("Maybe");
var result = alert.RunModal ();
添加的第一个按钮将是 默认按钮 ,如果用户按下 Enter 键,该按钮将激活。 返回的值将是一个整数,表示用户按下的按钮。 在本例中,将返回以下值:
- 正常 - 1000。
- 取消 - 1001。
- 也许 - 1002。
如果运行代码,将显示以下内容:
下面是与工作表相同的警报的代码:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.AddButton ("Ok");
alert.AddButton ("Cancel");
alert.AddButton ("Maybe");
alert.BeginSheetForResponse (this, (result) => {
Console.WriteLine ("Alert Result: {0}", result);
});
如果运行此代码,将显示以下内容:
重要
切勿向警报添加超过三个按钮。
显示“禁止显示”按钮
如果 Alert 的 ShowSuppressButton
属性为 true
,则警报将显示一个复选框,用户可以使用该复选框来禁止触发该警报的事件的后续发生。 以下代码显示带有抑制按钮的自由浮动警报:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.AddButton ("Ok");
alert.AddButton ("Cancel");
alert.AddButton ("Maybe");
alert.ShowsSuppressionButton = true;
var result = alert.RunModal ();
Console.WriteLine ("Alert Result: {0}, Suppress: {1}", result, alert.SuppressionButton.State == NSCellStateValue.On);
如果 的alert.SuppressionButton.State
NSCellStateValue.On
值为 ,则用户已选中“取消”复选框,否则他们没有选中。
如果运行代码,将显示以下内容:
下面是与工作表相同的警报的代码:
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.AddButton ("Ok");
alert.AddButton ("Cancel");
alert.AddButton ("Maybe");
alert.ShowsSuppressionButton = true;
alert.BeginSheetForResponse (this, (result) => {
Console.WriteLine ("Alert Result: {0}, Suppress: {1}", result, alert.SuppressionButton.State == NSCellStateValue.On);
});
如果运行此代码,将显示以下内容:
添加自定义子视图
警报具有一个 AccessoryView
属性,可用于进一步自定义警报,并为用户输入添加 文本字段 等内容。 以下代码使用添加的文本输入字段创建自由浮动警报:
var input = new NSTextField (new CGRect (0, 0, 300, 20));
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.AddButton ("Ok");
alert.AddButton ("Cancel");
alert.AddButton ("Maybe");
alert.ShowsSuppressionButton = true;
alert.AccessoryView = input;
alert.Layout ();
var result = alert.RunModal ();
Console.WriteLine ("Alert Result: {0}, Suppress: {1}", result, alert.SuppressionButton.State == NSCellStateValue.On);
此处的关键行将 var input = new NSTextField (new CGRect (0, 0, 300, 20));
创建一个新的 文本字段 ,我们将添加警报。 alert.AccessoryView = input;
将 文本字段 附加到警报,并调用 Layout()
方法,这是调整警报大小以适应新子视图所必需的。
如果运行代码,将显示以下内容:
下面是与工作表相同的警报:
var input = new NSTextField (new CGRect (0, 0, 300, 20));
var alert = new NSAlert () {
AlertStyle = NSAlertStyle.Informational,
InformativeText = "This is the body of the alert where you describe the situation and any actions to correct it.",
MessageText = "Alert Title",
};
alert.AddButton ("Ok");
alert.AddButton ("Cancel");
alert.AddButton ("Maybe");
alert.ShowsSuppressionButton = true;
alert.AccessoryView = input;
alert.Layout ();
alert.BeginSheetForResponse (this, (result) => {
Console.WriteLine ("Alert Result: {0}, Suppress: {1}", result, alert.SuppressionButton.State == NSCellStateValue.On);
});
如果运行此代码,将显示以下内容:
总结
本文详细介绍了在 Xamarin.Mac 应用程序中使用警报。 我们了解了警报的不同类型和用法、如何创建和自定义警报以及如何使用 C# 代码中的警报。