Windows 8 訊息對話方塊使用 MessageDialog

  • 1939
  • 0

Windows 8 訊息對話方塊使用 MessageDialog

 

1.MessageDialog不增加任何button,則會自動產生預設的關閉按鈕

  private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("測試訊息.", "TEST");
 
            await messageDialog.ShowAsync();
        
        }

image

2.MessageDialog自行增加button,按下該button後去執行該區塊程序,在下列舉例三種方式:

image

2.1

private async  void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("是否要繼續執行?", "確認");
         
            messageDialog.Commands.Add(new UICommand("是", (command) =>
            {
                //ToDo: 
            }));
            messageDialog.Commands.Add(new UICommand("否", (command) =>
            {
                //ToDo: 
            }));           
 
            await messageDialog.ShowAsync(); 
        }

2.2

  private async  void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("是否要繼續執行?", "確認");
 
            messageDialog.Commands.Add(new UICommand("是", null, 0));
            messageDialog.Commands.Add(new UICommand("否", null, 1));
 
            messageDialog.DefaultCommandIndex = 1;
 
            var commandChosen = await messageDialog.ShowAsync();
 
            switch (commandChosen.Id.ToString())
            {
                case "0":
                    //ToDo: 
                    break;
                case "1":
                    //ToDo: 
                    break;
            } 
        }

2.3

 private async void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var messageDialog = new MessageDialog("是否要繼續執行?", "確認");
                   
            messageDialog.Commands.Add(new UICommand("是", new UICommandInvokedHandler(this.CommandInvokedHandler1)));
            messageDialog.Commands.Add(new UICommand("否", new UICommandInvokedHandler(this.CommandInvokedHandler2)));
 
             await messageDialog.ShowAsync();
 
        }
        private void CommandInvokedHandler1(IUICommand command)
        {
             //ToDo: 
        }
        private void CommandInvokedHandler2(IUICommand command)
        {
            //ToDo: 
        }

3.將[否]按鈕的Index指定給此函數,按下鍵盤cancel執行否的事件。

messageDialog.CancelCommandIndex = 1;

4..將[是]按鈕的Index指定給此函數,按下鍵盤Enter執行是的事件。

messageDialog.DefaultCommandIndex = 0;

5.依照下圖程序執行按下Button4後顯示訊息方塊在按下[是]按鍵會執行RunOK函式發生執行錯誤,若使用上面2.2程式碼的方式可以避開這個問題

image