简体   繁体   English

MessageDialog在具有3个命令的Windows Phone 8.1上中断

[英]MessageDialog breaks on Windows Phone 8.1 with 3 commands

I'm trying to add a MessageDialog to a windows phone 8.1 app (WinRT) with 3 commands. 我正在尝试使用3个命令将MessageDialog添加到Windows Phone 8.1应用程序(WinRT)。 Looking at the documentation for MessageDialog: 查看MessageDialog的文档:

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

It says that "The dialog has a command bar that can support up to three commands", so I should think that wouldn't be a problem. 它说“对话框有一个命令栏,可以支持最多三个命令”,所以我认为这不会是一个问题。 I took their example (found on the documentation) and made a simple test app out of it, and it worked perfectly fine on both desktop and on windows phone. 我拿了他们的例子(在文档中找到)并制作了一个简单的测试应用程序,它在桌面和Windows手机上都运行得很好。 Then, I took the same example and added a single command to it: 然后,我采用相同的示例并向其添加了一个命令:

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

This works fine on a windows desktop app, but when I take the exact same code and try to use it for a windows phone app, it has no problem adding the 3rd command but when it gets to the await messageDialog.ShowAsync() line, it will crash with an unhandled exception. 这在Windows桌面应用程序上工作正常,但是当我使用完全相同的代码并尝试将其用于Windows手机应用程序时,添加第3个命令没有问题但是当它到达等待messageDialog.ShowAsync()行时,它会因未处理的异常而崩溃。 Interestingly, it does not crash in the same manner as a desktop app does when you add 4 commands. 有趣的是,当您添加4个命令时,它不会像桌面应用程序那样崩溃。 For that, it will throw the exception when you try to add the 4th command. 为此,当您尝试添加第4个命令时,它将抛出异常。 On the phone, it won't have a problem with that, but it won't work when it tries to show the messageDialog. 在手机上,它不会有问题,但是当它试图显示messageDialog时它将无法工作。

Am I missing something, or does the maximum number of commands on a MessageDialog quietly get bumped down from 3 to 2 when you're on a phone? 我错过了什么,或者当你在手机上时,MessageDialog上的最大命令数是否会从3下降到2?

You can only use two commands for the following event Windows.UI.Popups.MessageDialog . 您只能对以下事件Windows.UI.Popups.MessageDialog使用两个命令。

Here's an example.. 这是一个例子..

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.
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM