MessageDialog在Windows Phone 8.1上使用3个命令会出现故障。

12

我正在尝试向Windows Phone 8.1应用程序(WinRT)添加一个带有3个命令的MessageDialog。查看MessageDialog的文档:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog.aspx

文档中说:“对话框具有命令栏,最多可以支持三个命令”,所以我认为这不应该是个问题。我使用了他们在文档中提供的示例,并将其制作成了一个简单的测试应用程序,在桌面和Windows Phone上都完美地运行。然后,我将相同的示例添加了一个单独的命令:

var messageDialog = new MessageDialog("No internet connection has been found.");

// Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
messageDialog.Commands.Add(new UICommand(
    "Try again",
    new UICommandInvokedHandler(this.CommandInvokedHandler)));
messageDialog.Commands.Add(new UICommand(
    "Something else",
    new UICommandInvokedHandler(this.CommandInvokedHandler)));
messageDialog.Commands.Add(new UICommand(
    "Close",
    new UICommandInvokedHandler(this.CommandInvokedHandler)));

// Set the command that will be invoked by default
messageDialog.DefaultCommandIndex = 0;

// Set the command to be invoked when escape is pressed
messageDialog.CancelCommandIndex = 1;

// Show the message dialog
await messageDialog.ShowAsync();

这段代码在Windows桌面应用程序上运行良好,但是当我使用完全相同的代码尝试在Windows手机应用程序上运行时,它在添加第三个命令时没有问题,但当它到达 "await messageDialog.ShowAsync() "这一行时,就会崩溃并抛出一个未处理的异常。有趣的是,当您在桌面应用程序中添加第四个命令时,它不会像手机应用程序那样以相同的方式崩溃。对于桌面应用程序,它会在尝试添加第四个命令时抛出异常。但是在手机上,它不会有这个问题,但当它尝试显示messageDialog时,它无法正常工作。

我是否遗漏了什么,或者当您在手机上时,MessageDialog上的最大命令数会从3个悄然降至2个?


我认为在WinRT手机上的默认MessageDialog中,我们最多只能使用2个命令。 - Igor Ralic
好的,谢谢回复。我可以想出一种处理这个问题的方法,不过如果文档中包含这方面的信息会更好。 - Carson
1个回答

17

以下事件Windows.UI.Popups.MessageDialog仅可使用两个命令。

这里是一个例子...

private async void Button_Click(object sender, RoutedEventArgs e)
{
   //Message Box.
   MessageDialog msg = new MessageDialog("Here's the content/string.", "Hello!");

   //Commands
   msg.Commands.Add(new UICommand("Ok", new UICommandInvokedHandler(CommandHandlers)));
   msg.Commands.Add(new UICommand("Quit", new UICommandInvokedHandler(CommandHandlers)));

   await msg.ShowAsync();
   //end.
}

public void CommandHandlers(IUICommand commandLabel)
{
   var Actions = commandLabel.Label;
   switch (Actions)
   {
       //Okay Button.
       case "Ok" :
           MainpageName.Focus(Windows.UI.Xaml.FocusState.Pointer);
         break;
       //Quit Button.
       case "Quit" :
           Application.Current.Exit();
         break;
       //end.
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接