【问题标题】:How can i close MessageDialog from code in HTML5 Windows Store app?如何从 HTML5 Windows Store 应用程序中的代码关闭 MessageDialog?
【发布时间】:2013-03-10 22:47:31
【问题描述】:

如题,

如何从 HTML5 Windows Store 应用程序中的代码关闭 MessageDialog?

到目前为止我的代码:

var msg = new Windows.UI.Popups.MessageDialog("Please wait");
msg.commands.append(new Windows.UI.Popups.UICommand("OK",
    function (command) {
        ...
    }));
msg.showAsync();

然后我想从代码中关闭这个弹出窗口,我还没有找到任何方法 在像

这样的规范中
msg.close();

有办法吗?

谢谢

【问题讨论】:

  • 你的问题很模糊。请扩展它。你试过什么?你的代码看起来像什么?这是 Windows 应用商店应用 MessageDialog 类的文档:msdn.microsoft.com/library/windows/apps/BR208674
  • 不,我的问题非常清楚。我添加了明显被称为的行,你现在快乐吗?不,链接文档没有帮助。你至少读过我的问题吗?在链接之前您至少阅读过文档吗? ...

标签: javascript html windows-store-apps


【解决方案1】:

您的消息是“请稍候”这一事实向我表明,您可能希望为这项工作使用不同的工具。

如果您想要通知用户您正在后台执行某项他们需要等待的操作,请考虑改用进度控件,如下所述:

http://msdn.microsoft.com/en-us/library/windows/apps/hh465487.aspx

如果您使用进度控件,您既可以在文本标签中添加所需文本,也可以在完成您要求用户等待的任何任务时关闭进度控件。

为了回答您最初的问题,我认为没有任何 API 可以以编程方式关闭 MessageDialog,因为这会破坏此控件的交互模式,即应用显示消息,然后允许用户当他们准备好时将其关闭。

希望对您有所帮助。

有关 Windows 应用商店应用开发的更多信息,请注册App Builder

【讨论】:

  • 感谢您的建议,我已经考虑过进度控制,但消息对话框更适合 - 即使它破坏了交互模式。我的确切目的是当您邀请其他玩家时进入游戏的消息框,例如“您已邀请 XY,请稍候”,并且它有一个关闭按钮以停止等待但是如果其他玩家接受邀请,我想关闭我自己的对话。我想我现在会使用进度控制。谢谢!
【解决方案2】:

我认为您想使用弹出窗口,类似于 answer。该链接解决了一个稍微不同的问题,因为它会在超时后关闭弹出窗口。

但是,您应该能够定义您和用户都可以关闭的浮出控件。在这两种情况下,您最终都会调用类似:

flyout.winControl.hide(); // Dismiss the flyout

【讨论】:

  • 谢谢,我也找到了弹出控件,但我最终实现了自己的弹出控件,这样我也可以学到一些东西:P
  • 我相信如果用户点击/点击其他地方,弹出窗口会自行关闭...
【解决方案3】:

看看这个……

(function(){
"use strict"; 
    var page = WinJS.UI.Pages.define("/html/cancelcommand.html", { 
        ready: function (element, options) { 
            document.getElementById("cancelCommand").addEventListener("click", cancelCommand_Click, false); 
        } 
    }); 

    // Click handler for the 'cancelCommand' button. 
    // Demonstrates setting the command to be invoked when the 'escape' key is pressed. 
    // Also demonstrates retrieval of the label of the chosen command and setting a callback to a function. 
    // A message will be displayed indicating which command was invoked. 
    // In this scenario, 'Try again' is selected as the default choice, and the 'escape' key will invoke the command named 'Close' 
    function cancelCommand_Click() { 
        // Create the message dialog and set its content 
        var msg = new Windows.UI.Popups.MessageDialog("No internet connection has been found."); 

        // Add commands and set their command handlers 
        msg.commands.append(new Windows.UI.Popups.UICommand("Try again", commandInvokedHandler)); 
        msg.commands.append(new Windows.UI.Popups.UICommand("Close", commandInvokedHandler)); 

        // Set the command that will be invoked by default 
        msg.defaultCommandIndex = 0; 

        // Set the command to be invoked when escape is pressed 
        msg.cancelCommandIndex = 1; 

        // Show the message dialog 
        msg.showAsync(); 
    } 

    function commandInvokedHandler(command) { 
        // Display message 
        WinJS.log && WinJS.log("The '" + command.label + "' command has been selected.", "sample", "status"); 
    } 
}());

http://code.msdn.microsoft.com/windowsapps/Message-dialog-sample-00c928f5/sourcecode?fileId=50972&pathId=1064922824

【讨论】:

  • 谢谢,但唯一有用的部分是 cancelCommandIndex 设置,但这只会告诉页面在按下 Escape 时运行命令。但我不是在等待用户交互,我想自己从代码中关闭它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多