2015-12-21 28 views
1

我正在嘗試將相應消息框的異常處理添加到通用Windows應用程序。我只想使用TryParse或Try-Catch,但我無法弄清楚在通用Windows應用程序中與消息框等效的內容。我現在不特別擔心它的美學,只要我做的事符合UWP標準,所以我不會陷入前進的壞習慣。如何向通用Windows應用程序添加異常處理消息?

這裏是我的C#:

double INCHES = 1; 
    double FEET = 12; 
    double YARDS = 36; 

    double userDist, convertDist, distFrom, distTo; 
    string unitOfMeasure; 

    private void convertButton_Click(object sender, RoutedEventArgs e) 
    { 
     unitOfMeasure = null; 
     distFrom = 1; 
     distTo = 1; 
     if (inputTextBox.Text != "") 
     { 
      userDist = double.Parse(inputTextBox.Text); 
      if (listBox1.SelectedIndex >= 0 || listBox2.SelectedIndex >= 0) 
      { 
       switch (listBox1.SelectedIndex) 
       { 
        case 0: 
         distFrom = INCHES; 
         unitOfMeasure = " in"; 
         break; 
        case 1: 
         distFrom = FEET; 
         unitOfMeasure = " ft"; 
         break; 
        case 2: 
         distFrom = YARDS; 
         unitOfMeasure = " yd"; 
         break; 
       } 
       switch (listBox2.SelectedIndex) 
       { 
        case 0: 
         distTo = INCHES; 
         unitOfMeasure = " in"; 
         break; 
        case 1: 
         distTo = FEET; 
         unitOfMeasure = " ft"; 
         break; 
        case 2: 
         distTo = YARDS; 
         unitOfMeasure = " yd"; 
         break; 
       } 
       convertDist = (userDist * distFrom)/distTo; 
       outputTextBlock.Text = convertDist.ToString("n2") + unitOfMeasure; 
      } 
      else 
      { 
       //MessageDialog dialog = new MessageDialog("Please select 'From' and 'To' units."); 
      } 
     } 
     else 
     { 
      //MessageDialog dialog = new MessageDialog("Please input a number to convert."); 
     }   
    } 

    private void clearButton_Click(object sender, RoutedEventArgs e) 
    { 
     inputTextBox.Text = ""; 
     listBox1.SelectedIndex = -1; 
     listBox2.SelectedIndex = -1; 
     outputTextBlock.Text = ""; 
     distFrom = 1; 
     distTo = 1; 
    } 

    private void exitButton_Click(object sender, RoutedEventArgs e) 
    { 
     App.Current.Exit(); 
    } 

回答

0

我想你註釋掉的代碼應該工作,如果您還添加 -

等待dialog.ShowAsync();

0

像布賴恩說

MessageDialog dialog = new MessageDialog("Please select 'From' and 'To' units."); 
    await dialog.ShowAsync(); 

應該工作。我還補充說,您需要將convertButton_Click標記爲異步方法:

private async void convertButton_Click(object sender, RoutedEventArgs e) 
+0

太棒了。現在,我忘記了在這個項目中我曾經做過這件事,但當我重新開始時,我得到了一個粗魯的覺醒:我已經添加了:使用Windows.UI.Popups;到MainPage C#和App C#。我應該在哪裏具體說明未來? –

+0

無論你打算使用MessageDialog類,你都必須添加它。 – Seine

相關問題