UWP 应用程序的 MessageDialog class 是否支持移动设备上的三个按钮?

Does the MessageDialog class for UWP Apps support three buttons on Mobile?

我正在创建一个简单的程序来读取 Windows Phone 上的文本文件。我决定将其设为通用 Windows 平台 (UWP) 应用程序。

在应用程序中,我有一个非常简单的 MessageDialog,有三个选项,是、否、取消。它在桌面和模拟器中完美运行。但是,在使用实际设备进行测试时,ShowAsync 方法失败并显示消息:"Value does not fall in the expected range".

只有在对话框中注册了两个以上的命令时才会发生这种情况。 MessageDialog class 是否真的支持最多三个命令 - 正如文档所建议的那样 - 或者这是否仅适用于桌面设备上的 UWP 应用程序 运行?

文档似乎缺少有关 Mobile 的信息(实际上 API 应该在这里做得更好)。

对于移动设备,如果您按下返回键,您会得到一个 null return 值,因此您可以这样做(不推荐编码模式,但我认为最好的):

async Task Test()
{
  const int YES = 1;
  const int NO = 2;
  const int CANCEL = 3;

  var dialog = new MessageDialog("test");
  dialog.Commands.Add(new UICommand { Label = "yes", Id = YES });
  dialog.Commands.Add(new UICommand { Label = "no", Id = NO });

  // Ugly hack; not really how it's supposed to be used.
  // TODO: Revisit if MessageDialog API is updated in future release
  var deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily;
  if (deviceFamily.Contains("Desktop"))
  {
    dialog.Commands.Add(new UICommand { Label = "cancel", Id = CANCEL });
  }
  // Maybe Xbox 'B' button works, but I don't know so best to not do anything
  else if (!deviceFamily.Contains("Mobile"))
  {
    throw new Exception("Don't know how to show dialog for device "
      + deviceFamily);
  }

  // Will return null if you press Back on Mobile
  var result = await dialog.ShowAsync();

  // C# 6 syntactic sugar to avoid some null checks
  var id = (int)(result?.Id ?? CANCEL);

  Debug.WriteLine("You chose {0}", id);
}

目前,文档中有一个明确的声明:

The dialog has a command bar that can support up to 3 commands in desktop apps, or 2 commands in mobile apps.

悲伤但真实:在手机上,只有两个命令。需要更多?请改用 ContentDialog。