2016-11-29 64 views
0

我在我的uwp應用程序中有一個數據透視控制。在我寫過的pivot_selectionchanged事件中顯示消息對話。UWP應用程序崩潰而不是顯示消息對話框

當我點擊透視項時生成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(); 
} 
+2

也許環繞你的代碼的try/catch語句,這樣你可以找到它崩潰時出現這種情況的例外呢?異常消息可能會幫助您瞭解實際發生的情況。 – Streltsov

+0

您必須發佈應用程序的例外以獲取更多詳細信息。謝謝! – RicardoPons

+0

爲什麼人們在try/catch語句中建議使用包裝代碼來查找異常?這完全沒有必要,而且是一個可怕的想法! **當引發異常時,您的調試器將中斷並讓您立即看到它。你不需要一堆顯示消息框的catch塊。**全局的,未處理的異常處理程序已經做到了!所有添加try/catch塊的做法都有助於確保異常處於無聲狀態,並且您從來沒有很好的機會去調試它們。 –

回答

1

這可能是因爲你實際上沒有等待消息表明,返回類型從無效改變到任務:

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... 
+0

openbillpivot_selectionchanged是一個事件,所以我們不能改變這個事件。 – Sagar

+0

它是一個事件處理程序,我看不出有什麼理由不能將其標記爲異步 – RTDev

相關問題