[Windows 8 App]對話式方框提示訊息

[Windows 8 App]對話式方框提示訊息

在操作應用程式的過程中,向使用者進行詢問並希望得到使用者的回應時,就可以使用對話式方框

下面將透過範例做對話式方框提示訊息的介紹

首先,新增一個專案,名稱為【MessageDialogPage】

打開【MessageDialogPage.xaml】,在Grid的設計畫面中放進一個TextBlock,並命名為【OutPut】

用來顯示使用者的操作

下面是TextBlock的程式碼:


<TextBlock x:Name="OutPut" FontSize="30"   HorizontalAlignment="Left" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Margin="454,281,0,0" Height="145" Width="507"/>

 

接下來,開啟【MainPage.xaml.cs】

由於程式碼中新增對話方框會使用式MessageDialog類別

所以,我們必須 using Windows.UI.Popups 在前端的程式碼中

新增完畢後,在OnNavigatedTo方法中寫程式碼

程式碼如下:


async protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            MessageDialog messageDialog = new MessageDialog("此頁線將獲得您的地理位置訊息,是否繼續瀏覽此頁面");
            messageDialog.Title = "警告! ";
            UICommand continueCommand = new UICommand("繼續", (command) => 
            {
                OutPut.Text = "用戶點擊了'繼續' 按鈕";
            });
            messageDialog.Commands.Add(continueCommand);

            UICommand closeCommand = new UICommand("關閉", (command) =>
            {
                OutPut.Text = "用戶點擊了'關閉' 按鈕";
            });
            messageDialog.Commands.Add(closeCommand);
            await messageDialog.ShowAsync();

        }

 

在上面的程式碼中,使用MessageDialog類別的對象messageDialog來定義對話框

使用messageDialog的Title屬性設置對話框的標題

關於UICommand類別的使用可以參考此文章:

[Windows 8 App]捷徑菜單

 

完整的【MainPage.xaml.cs】程式碼:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 空白頁項目範本已記錄在 http://go.microsoft.com/fwlink/?LinkId=234238

namespace MessageDialogPage
{
    /// <summary>
    /// 可以在本身使用或巡覽至框架內的空白頁面。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此頁面即將顯示在框架中時叫用。
        /// </summary>
        /// <param name="e">描述如何到達此頁面的事件資料。Parameter
        /// 屬性通常用來設定頁面。</param>
        async protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            MessageDialog messageDialog = new MessageDialog("此頁線將獲得您的地理位置訊息,是否繼續瀏覽此頁面");
            messageDialog.Title = "警告! ";
            UICommand continueCommand = new UICommand("繼續", (command) => 
            {
                OutPut.Text = "用戶點擊了'繼續' 按鈕";
            });
            messageDialog.Commands.Add(continueCommand);

            UICommand closeCommand = new UICommand("關閉", (command) =>
            {
                OutPut.Text = "用戶點擊了'關閉' 按鈕";
            });
            messageDialog.Commands.Add(closeCommand);
            await messageDialog.ShowAsync();

        }
    }
}

 

專案的執行畫面:

 

執行後會跳出下面的畫面

256

 

這是點擊繼續後顯示的畫面

257

 

這是點擊關閉後顯示的畫面

258