【问题标题】:UWP app crashing instead of showing message dialogue boxUWP 应用程序崩溃而不是显示消息对话框
【发布时间】:2017-04-13 11:35:54
【问题描述】:

我的 uwp 应用程序中有一个枢轴控件。在 pivot_selectionchanged 事件中,我写了显示消息对话。

当我单击数据透视项目生成 uwp 应用程序包后,应该会显示一些警报消息对话框,但它没有显示,而是此时应用程序崩溃了。 这仅适用于某些机器。 有谁知道原因吗?

我写的代码是

private void OpenBillPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    bool Isreturn = false;
    bool IsExchange = false;

    Isreturn = checkUserPermission(Constants.Return);
    IsExchange = checkUserPermission(Constants.Exchange);
    if (Application.Current.Resources[Constants.RETURNS].ToString() == Constants.FALSE_CAMELCASE)
        Isreturn = false;
    else
        Isreturn = true;
    if (Application.Current.Resources[Constants.EXCHANGES].ToString() == Constants.FALSE_CAMELCASE)
        IsExchange = false;
    else
        Isreturn = true;

    if ((txtblStatus.Text == "Cancelled" || txtblStatus.Text=="Draft") && (OpenBillPivot.SelectedIndex == 1 || OpenBillPivot.SelectedIndex == 2 || OpenBillPivot.SelectedIndex == 3))
    {
        TransactionDetails.Visibility = Visibility.Collapsed;
        ReturnDetails.Visibility = Visibility.Collapsed;
        ExchangeDetails.Visibility = Visibility.Collapsed;
        //SimpleMessageDialog(ResourceLoader.GetForCurrentView().GetString("PLEASE_CLEAR_THE_BILL"), ResourceLoader.GetForCurrentView().GetString("ALERT"));
    }

    else if (OpenBillPivot.SelectedIndex == 2)
    {
        if ((txtblStatus.Text == "Pending" && txtblBillDue.Text != Constants.ZERO))
        {
            ReturnDetails.Visibility = Visibility.Collapsed;
            ExchangeDetails.Visibility = Visibility.Collapsed;
            SimpleMessageDialog(ResourceLoader.GetForCurrentView().GetString("PLEASE_CLEAR_THE_BILL"), ResourceLoader.GetForCurrentView().GetString("ALERT"));
        }

        else
        {
            if (!Isreturn)
            {
                ReturnDetails.Visibility = Visibility.Collapsed;
                SimpleMessageDialog("Access Denied", ResourceLoader.GetForCurrentView().GetString("ALERT"));
            }
            else
                ReturnDetails.Visibility = Visibility.Visible;
        }

    }

和消息对话代码:

private async void SimpleMessageDialog(string Message, string Title)
{
    MessageDialog dialog = new MessageDialog(Message, Title);
    dialog.CancelCommandIndex = 1;
    await dialog.ShowAsync();
}

【问题讨论】:

  • 也许用 try/catch 语句包围你的代码,这样你就可以找到崩溃时发生的异常?异常消息可能会帮助您了解实际发生的情况。
  • 您必须发布您的应用程序的异常以获取更多详细信息。谢谢!
  • 为什么人们建议在 try/catch 语句中包装代码来查找异常?这是完全没有必要的,也是一个糟糕的主意! 当一个异常被抛出时,你的调试器会进入并让你在那个时候立即看到它。您不需要一堆显示消息框的 catch 块。 全局的、未处理的异常处理程序已经这样做了!添加一堆 try/catch 块所做的只是帮助确保异常被静音,并且您永远没有机会调试它们。

标签: c# .net windows uwp win-universal-app


【解决方案1】:

这可能是因为您实际上并没有等待消息显示,将返回类型从 void 更改为 Task:

private async Task SimpleMessageDialog(string Message, string Title)
{
    MessageDialog dialog = new MessageDialog(Message, Title);
    dialog.CancelCommandIndex = 1;
    await dialog.ShowAsync();
}

等待它:

await SimpleMessageDialog(ResourceLoader.GetForCurrentView().GetString("PLEASE_CLEAR_THE_BILL"), ResourceLoader.GetForCurrentView().GetString("ALERT"));

您还需要将方法更改为异步:

private async void OpenBillPivot_SelectionChanged...

【讨论】:

  • openbillpivot_selectionchanged 是一个事件,所以我们无法更改它。
  • 它是一个事件处理程序,我看不出有任何理由不能将其标记为异步
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
相关资源
最近更新 更多