1

我开发了一个通用应用程序,它使用MVVM-Light。我叫Web服务从的ViewModels,我扔通过在Web服务到的ViewModels呼叫遇到的例外:超时错误的URL,服务器异常,...在Windows Phone 8.1下多次调用MessageDialog会导致崩溃

我创建了一个类“ExceptionsMsgHelper .cs“,它通过MessageDialog集中显示每个例外的消息。

我的主页基于包含多个数据的Pivot:某些WebServices被异步调用。如果我通过类“ExceptionsMsgHelper.cs”在MessageDialog中显示异常,则会遇到崩溃,而以前的异常也会显示在另一个MessageDialog中。

这是我原来的类的一部分:

public class ExceptionsMsgHelper 
    { 
     public async static void MsgboxWebserviceErrors(WebServiceErrorsException wseE, string errors) 
     { 
      Windows.UI.Popups.MessageDialog msgbox = 
      new Windows.UI.Popups.MessageDialog("The Websercice '" + wseE.WebService + "' has returned errors : \n" + errors, 
       "Unexpected data");   
      await msgbox.ShowAsync(); 
     } 
    } 

=>如果我叫两次 “msgbox.ShowAsync()”,我得到了一下 “System.UnauthorizedAccessException” 异常:有消息“访问被拒绝。(异常来自HRESULT:0X80070005(E_ACCESSDENIED))”

我这样寻找解决方案,以解决它:

的代码是:

public class ExceptionsMsgHelper 
    { 
     public async static void MsgboxWebserviceErrors(WebServiceErrorsException wseE, string errors) 
     { 
      Windows.UI.Popups.MessageDialog msgbox = 
      new Windows.UI.Popups.MessageDialog("The Websercice '" + wseE.WebService + "' has returned errors : \n" + errors, 
       "Unexpected data");   
      CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; 
      dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => 
      { 
       await msgbox.ShowAsync(); 
      }); 
     } 
    } 

=>但我总能在相同的异常。

有了这个代码建议:

public class ExceptionsMsgHelper 
    { 
     private static IAsyncOperation<IUICommand> messageDialogCommand = null; 
     public async static Task<bool> ShowDialog(MessageDialog dlg) 
     { 

      // Close the previous one out 
      if (messageDialogCommand != null) 
      { 
       messageDialogCommand.Cancel(); 
       messageDialogCommand = null; 
      } 

      messageDialogCommand = dlg.ShowAsync(); 
      await messageDialogCommand; 
      return true; 
     } 

     public async static void MsgboxWebserviceErrors(WebServiceErrorsException wseE, string errors) 
     { 
      Windows.UI.Popups.MessageDialog msgbox = 
      new Windows.UI.Popups.MessageDialog("The Websercice '" + wseE.WebService + "' has returned errors : \n" + errors, 
       "Unexpected data");   
      CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher; 
      dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => 
      { 
       await ShowDialog(msgbox); 
      }); 
     }   
    } 

=>但是在这种情况下,我也总是得到相同的异常。

的代码现在:

public class ExceptionsMsgHelper 
    { 
     public async static void MsgboxWebserviceErrors(WebServiceErrorsException wseE, string errors) 
     { 
      Windows.UI.Popups.MessageDialog msgbox = 
      new Windows.UI.Popups.MessageDialog("The Websercice '" + wseE.WebService + "' has returned errors : \n" + errors, 
       "Unexpected data"); 
      await MessageDialogExtensions.ShowAsyncQueue(msgbox); 
     } 
    } 

    public static class MessageDialogExtensions 
    { 
     private static TaskCompletionSource<MessageDialog> _currentDialogShowRequest; 
     public static async Task<IUICommand> ShowAsyncQueue(this MessageDialog dialog) 
     { 
      if (!Window.Current.Dispatcher.HasThreadAccess) 
      { 
       throw new InvalidOperationException("This method can only be invoked from UI thread."); 
      } 

      while (_currentDialogShowRequest != null) 
      { 
       await _currentDialogShowRequest.Task; 
      } 

      var request = _currentDialogShowRequest = new TaskCompletionSource<MessageDialog>(); 
      var result = await dialog.ShowAsync(); 
      _currentDialogShowRequest = null; 
      request.SetResult(dialog); 

      return result; 
     } 
    private static IAsyncOperation<IUICommand> messageDialogCommand = null; 
    public async static Task<bool> ShowDialog(this MessageDialog dlg) 
    { 

     // Close the previous one out 
     if (messageDialogCommand != null) 
     { 
      messageDialogCommand.Cancel(); 
      messageDialogCommand = null; 
     } 

     messageDialogCommand = dlg.ShowAsync(); 
     await messageDialogCommand; 
     return true; 
    } 

    #endregion 
    } 

=>并且这适用于我。

但喜欢说,它的作者,这可能不是最好的解决办法:

关闭现有的对话,当你需要打开一个新的。这是最简单的选择,也可能是最好的选择,尽管您可能会冒失败的风险取决于您的对话是关于什么重要的对话。 排队对话使旧的不会被解雇,但新的解雇之后就会出现。这将确保用户关闭所有对话框,但如果您的应用程序可以以某种方式开始显示数百个对话框,则可能会出现问题。 如果还没有显示,请只打开一个新的。现在这冒着一条新消息未被显示的风险,这听起来比第一个选项更成问题。

=>我想知道为什么我不能申请一个2个第一解决方案,似乎更适应

回答

2

Ofcourse就可以不显示2个或更多时消息对话框同时(windows手机限制)。此外,Windows Phone 8.1上的MesssageDialog可能存在错误,无法关闭。

如果关闭上一个对话框将是您的解决方案,请尝试使用ContentDialog而不是MessageDialog。检查我在这个主题的答案:Closing MessageDialog programatically in WP 8.1 RT

我认为它解决了你的问题。

相关问题