按照Stack Overflow中的示例,我组合了一个MessageDialog来显示我的用户错误消息。在模拟器中,它工作得很好。
在手机上,它会被击穿,屏幕上的MessageDialog只会闪烁片刻,甚至会击穿我放入的Task.Delay作为变通办法。
谁能给我解释一下发生了什么事,或者给我指个正确的方向?
附注:我还在这里尝试了每篇文章的ContentDialog。它甚至不显示消息文本。
下面是一个代码片段:
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!!!
}发布于 2016-03-08 14:05:05
问题的原因很简单--你声明了async void方法--避免这一点,这应该只在特殊情况下使用,例如事件。在您已有的代码中,您的程序不会在您调用该方法的地方停止:
ShowAndGo("Message code", "Message Text", false);
Debug.WriteLine("Something happening");它可能会显示一条消息,但它能存活多长时间取决于您的后续代码。解决此问题的方法是将方法从void更改为Task and await
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当然,这需要所有的异步,但这可能就是你的程序应该看起来的样子。
https://stackoverflow.com/questions/35857566
复制相似问题