【问题标题】:Windows Phone 8.1 Modal Dialog. How?Windows Phone 8.1 模式对话框。如何?
【发布时间】:2016-03-08 01:58:28
【问题描述】:

按照 Stack Overflow 中的示例,我整理了一个 MessageDialog 来显示我的用户错误消息。 在模拟器中,它工作正常。

在手机上,它直接闪过,仅在屏幕上闪现 MessageDialog 片刻,甚至闪过我作为解决方法输入的 Task.Delay。

有人可以向我解释发生了什么,或者指出正确的方向吗?

附言我还在这里为每篇文章尝试了 ContentDialog。这甚至不显示消息文本。

这是一个代码sn-p:

public static async void ShowAndGo (String MessCode, String MessText, Boolean Xit)
{
    String Mess = "";                               // Start out with an empty Message to tell Joe User.
    String Title = "";                              // And an empty title too.

    if (MessCode != "")                             // If we're sent a Message "Code,"
        Mess = App.ResLdr.GetString (MessCode) + Cx.ld + Cx.ld; // turn it into text, culturally-aware.
    Mess += MessText;                               // Stick MessText onto the end of it.

    if (Xit)
        Title = App.ResLdr.GetString ("OhSnap");    // If we're goin' down, curse a little.
    else
        Title = App.ResLdr.GetString ("NoProb");    // If it's just informational, no problem-o.

    MessageDialog messageDialog = new MessageDialog (Mess, Title);
    await messageDialog.ShowAsync ();               // IT FREAKING ISN'T STOPPING HERE!!!
    Task.Delay (10000).Wait ();                     // Wait 10 seconds with error message on the screen.
                                                    // AND IT FREAKING DOESN'T STOP HERE EITHER!!!
}

【问题讨论】:

    标签: c# windows-phone-8.1 modal-dialog messagedialog


    【解决方案1】:

    您的问题的原因很简单 - 您正在声明 async void 方法 - 避免这种情况,这应该只在特殊情况下使用,例如事件。在您拥有的代码中,您的程序不会在您调用该方法的地方停止:

    ShowAndGo("Message code", "Message Text", false);
    Debug.WriteLine("Something happening");
    

    它可能会显示一条消息,但它会存活多长时间取决于您的进一步代码。对此的补救措施是将方法从 void 更改为 Taskawait

    public static async Task ShowAndGo (String MessCode, String MessText, Boolean Xit)
    {  /* method */ }
    
    //invoke:
    await ShowAndGo("Message code", "Message Text", false);
    Debug.WriteLine("Something happening"); // now it should wait till user clicks OK
    

    当然,这需要一直异步,但可能你的程序应该是这样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 2023-04-03
      相关资源
      最近更新 更多