Xamarin.Mac のアラート

この記事では、Xamarin.Mac アプリケーションでのアラートの操作について説明します。 C# コードからのアラートの作成と表示、およびユーザーの操作への応答について説明します。

Xamarin.Mac アプリケーションで C# と .NET を使用する場合は、開発者が作業しているのと Xcode と同じアラートにObjective-Cアクセスできます。

アラートは、重大な問題 (エラーなど) が発生したとき、または警告 (ファイルの削除の準備など) として表示される特殊な種類のダイアログです。 アラートはダイアログであるため、閉じる前にユーザーの応答も必要です。

An example alert

この記事では、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 ();

上記のコードでは、警告アイコン、タイトル、警告メッセージ、1 つの [OK] ボタンにアプリケーション アイコンが重ね合わされたアラートが表示されます。

An alert with a OK button

Apple には、アラートをカスタマイズするために使用できるいくつかのプロパティが用意されています。

  • AlertStyle は 、アラートの種類を次のいずれかとして定義します。
    • 警告 - 重大ではない現在または差し迫ったイベントをユーザーに警告するために使用されます。 これが既定のスタイルです。
    • 情報 - 現在または差し迫ったイベントについてユーザーに警告するために使用されます。 現時点では、警告と情報の間に目に見える違いはありません
    • 重大 - 差し迫ったイベント (ファイルの削除など) の重大な結果についてユーザーに警告するために使用されます。 この種類のアラートは慎重に使用する必要があります。
  • MessageText - これはアラートのメインメッセージまたはタイトルであり、ユーザーに状況をすばやく定義する必要があります。
  • 情報テキスト - これは、状況を明確に定義し、実行可能なオプションをユーザーに提示する必要があるアラートの本文です。
  • アイコン - カスタム アイコンをユーザーに表示できるようにします。
  • HelpAnchor & ShowsHelp - アラートをアプリケーション HelpBook に関連付け、アラートのヘルプを表示できるようにします。
  • ボタン - 既定では、アラートには [OK]ボタンしかありませんが、Buttons コレクションでは必要に応じて選択肢をさらに追加できます。
  • ShowsSuppressionButton - ユーザーがトリガーしたイベントの後続の発生に対するアラートを抑制するために使用できるチェックボックスが表示される場合true
  • AccessoryView - アラートに別のサブビューをアタッチして、データ入力用のテキスト フィールドの追加などの追加情報を提供できます。 新しい AccessoryView を設定した場合、または既存の AccessoryView を変更する場合は、メソッドを Layout() 呼び出して、アラートの表示レイアウトを調整する必要があります。

アラートの表示

アラートを表示するには、自由浮動またはシートとして 2 つの異なる方法があります。 次のコードは、アラートを自由浮動として表示します。

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 ();

このコードを実行すると、次の内容が表示されます。

A simple alert

次のコードは、シートと同じアラートを表示します。

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);

このコードを実行すると、次の内容が表示されます。

An alert displayed as a sheet

アラート ボタンの操作

既定では、アラートには [OK] ボタンのみが表示されます。 ただし、それに限定されるわけではありません。追加のボタンを Buttons コレクションに追加することで作成できます。 次のコードでは、[OK]、[キャンセル] ボタン、および [可能性あり] ボタンを使用して、フリーフローティング アラートを作成します。

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 キーを 押すとアクティブ化される既定 のボタンになります。 返される値は、ユーザーが押したボタンを表す整数になります。 この場合、次の値が返されます。

  • OK - 1000。
  • キャンセル - 1001。
  • 多分 - 1002。

コードを実行すると、次の内容が表示されます。

An alert with three button options

シートと同じアラートのコードを次に示します。

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);
});

このコードを実行すると、次の内容が表示されます。

A three button alert displayed as a sheet

重要

アラートに 3 つ以上のボタンを追加しないでください。

[抑制] ボタンの表示

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);

値が is のalert.SuppressionButton.StateNSCellStateValue.On場合、ユーザーは [抑制] チェック ボックスをチェック。それ以外の場合は表示されません。

コードを実行すると、次の内容が表示されます。

An alert with a suppress button

シートと同じアラートのコードを次に示します。

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);
});

このコードを実行すると、次の内容が表示されます。

An alert with a suppress button display as a sheet

カスタム SubView の追加

アラートには、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()行います。これは、新しいサブビューに収まるようにアラートのサイズを変更するために必要です。

コードを実行すると、次の内容が表示されます。

If we run the code, the following will be displayed

シートと同じアラートを次に示します。

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);
});

このコードを実行すると、次のコードが表示されます。

An alert with a custom view

まとめ

この記事では、Xamarin.Mac アプリケーションでのアラートの操作について詳しく説明しました。 さまざまな種類とアラートの使用方法、アラートを作成およびカスタマイズする方法、C# コードでアラートを操作する方法について説明しました。