[Windows Phone] 資料載入時要有載入中畫面

  • 2882
  • 0
  • 2013-12-06

[好的 Windows Phone App 通常具備的特點中] 文中提到資料載入時要有載入中畫面,想要達成這樣的功能,可以操作 SystemTray 搭配 ProgressIndicator 達成。

 

前言

[好的 Windows Phone App 通常具備的特點中] 文中提到
http://blogs.msdn.com/b/hermanwu/archive/2012/11/24/windows-phone-app.aspx
資料載入時要有載入中畫面


想要達成這樣的功能,可以操作 SystemTray 搭配 ProgressIndicator 達成
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626537%28v=vs.105%29.aspx

 

實作

新增專案,在 MainPage.xaml 中,加入 CheckBox 控制項。



切換至程式碼,我們搭配 ProgressIndicator 提供用於與應用程式的 SystemTray 做進度指示的方法和屬性。
http://msdn.microsoft.com/en-US/library/windowsphone/develop/microsoft.phone.shell.progressindicator%28v=vs.105%29.aspx

先引用 Microsoft.Phone.Shell

 


using Microsoft.Phone.Shell;

 

 

 建立 ProgressIndicator 並設定給 SystemTray 程式碼

 


            // 建立 ProgressIndicator 並設定給 SystemTray
            ProgressIndicator pi = new ProgressIndicator();
            pi.Text = "Loading...";
            pi.IsIndeterminate = true;
            pi.IsVisible = true;
            SystemTray.SetProgressIndicator(this, pi);

 

以下是程式完整程式碼,其中 CheckBox 包含兩個事件 Checked 和 Unchecked,當 Checked 觸發時,顯示 System Tray,當 Unchecked 事件觸發時,隱藏 System Tray。

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace SystemTrayApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 建構函式
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            // 建立 ProgressIndicator 並設定給 SystemTray
            ProgressIndicator pi = new ProgressIndicator();
            pi.Text = "Loading...";
            pi.IsIndeterminate = true;
            pi.IsVisible = true;
            SystemTray.SetProgressIndicator(this, pi);
            
            // Checkbox 屬性與事件
            this.cbShowSystemTray.IsChecked = true;
            this.cbShowSystemTray.Checked += cbShowSystemTray_Checked;
            this.cbShowSystemTray.Unchecked += cbShowSystemTray_Unchecked;
        }

        void cbShowSystemTray_Checked(object sender, RoutedEventArgs e)
        {
            // SystemTray 顯示
            SystemTray.IsVisible = true;
        }

        void cbShowSystemTray_Unchecked(object sender, RoutedEventArgs e)
        {
            // SystemTray 隱藏
            SystemTray.IsVisible = false;
        }
    }
}

 

 

執行程式,一開始就會顯示 Loading...


取消勾選 CheckBox 後 Loading ... 消失

 

範例下載

SystemTrayApp.zip