2012-12-16 16 views
0

我有一個應用程序需要用戶注意,當用戶至少退出一次應用程序。所以我得到下面的代碼來顯示一個消息框。我不知道的是,如果用戶閱讀了該消息,我該如何真正退出該應用?因爲它看起來後面的關鍵事件總是會來到我的設置(OnBackKeyPress)或什麼是一個好方法來處理顯示一個消息框而不會弄亂重寫的BackKey?因爲如果屏幕上出現另一個彈出窗口並且用戶按下了回車鍵,那麼如果我嘗試自己處理backkey,則看起來有些異常。Windows Phone 8:如果按退格鍵,退出應用程序之前如何顯示messagebox?

我的理想情況是應用程序會在他/她按下消息框的退出按鈕時立即關閉。如果按下取消,它將返回而不退出。請幫忙。謝謝!

東西我用......但都不盡如人意

private void OnBackKeyPressed(object sender, CancelEventArgs e) 
    { 
     e.Cancel = true; 

     CheckBox checkBox = new CheckBox() 
     { 
      Content = "Do not ask me again", 
      Margin = new Thickness(0, 14, 0, -2) 
     }; 

     TiltEffect.SetIsTiltEnabled(checkBox, true); 

     CustomMessageBox messageBox = new CustomMessageBox() 
     { 
      Caption = "Would you like to stop and exit?", 
      Message = 
       "If you want to continue listen music while doing other stuff, please use Home key instead of Back key", 
      Content = checkBox, 
      LeftButtonContent = "Exit", 
      RightButtonContent = "Cancel", 
     }; 

     messageBox.Dismissed += (s1, e1) => 
     { 
      switch (e1.Result) 
      { 
       case CustomMessageBoxResult.LeftButton: //Exit 
        return;// What to do here?? 
       case CustomMessageBoxResult.RightButton: //Cancel 
       case CustomMessageBoxResult.None: 
        break; 
       default: 
        break; 
      } 
     }; 
     messageBox.Show(); 
    } 

} 

回答

3

如果您確實需要按鈕說「退出」,您將不得不使用ColinE或Paul Annetts解決方案。個人而言,我會嘗試以另一種方式制定的消息,這樣使用正常的消息框:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
{ 
    string caption = "Stop music and exit?"; 
    string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?"; 
    e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel); 

    base.OnBackKeyPress(e); 
} 

這將顯示一個消息框,但「確定」和「取消」按鈕來代替,如果用戶操作將直接退出「好」。

而對於「不要再問」,也許在設置頁面上添加設置。如果用戶希望再次看到對話,則首次在「確定」和「取消」之間選擇時添加第二個對話框。

+0

謝謝!所有解決方案都適合我! – thsieh

0

對於WP8可以調用Application.Current.Terminate()以編程方式退出程序。對於WP7,這是不可用的,你必須選擇科林給出的選項之一。

但是,如果你走這條路線,你應該小心滿足Microsoft商店的認證要求,因爲如果你的應用程序沒有按預期行事,它可能會被拒絕。

1

我已經使用自定義消息框做了同樣的事情。 修改您這樣的代碼:

messageBox.Dismissed += (s1, e1) => 
    { 
     switch (e1.Result) 
     { 
      case CustomMessageBoxResult.LeftButton: //Exit 
       App.Current.Terminate(); //Put this line followed by break 
       break; 
      case CustomMessageBoxResult.RightButton: //Cancel 
       break; 
      default: 
       break; 
     } 
    }; 

當用戶按下退出你的應用程序將終止,當貼近,「左」按鈕的默認操作是關閉該消息框。 這應該工作只要你放線

break; 

在正確的地方

希望這有助於

+0

只是做了一些進一步的測試。 這裏唯一的問題是,如果後退按鈕被按下並且CustomMessageBox打開,如果用戶再次按下後退按鈕,應用程序將崩潰。 將試圖弄清楚並解決它。 – Tanuj

0

你可以簡單地取消後退鍵的情況下按第一,然後基於按鈕點擊消息框中可以採取相應的操作如下:

private void BackKey_Pressed(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
     e.Cancel = true; 
     MessageBoxResult messageBoxResult = MessageBox.Show("Text", "Caption", MessageBoxButton.OKCancel); 
     if (messageBoxResult == MessageBoxResult.OK) 
     { 
      Application.Current.Terminate(); 
     } 
} 
相關問題