Windows Phone 7程序等待頁面的處理
程序啟動通常會有一個等待的過程,在這個過程中可以通過使用Popup控件配合BackgroundWorker類啟動后臺線程來實現。
控件的代碼
PopupSplash.xaml
- <UserControl x:Class="ProgressSplashScreen.PopupSplash"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- FontFamily="{StaticResource PhoneFontFamilyNormal}"
- FontSize="{StaticResource PhoneFontSizeNormal}"
- Foreground="{StaticResource PhoneForegroundBrush}"
- d:DesignHeight="800" d:DesignWidth="480">
- <Grid x:Name="LayoutRoot" Background="White" Width="480" Height="800">
- <ProgressBar HorizontalAlignment="Left" Margin="0,755,0,0" Name="progressBar1" Width="480" Background="DarkRed" />
- <Image Height="757" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="480" Source="/ProgressSplashScreen;component/wuyuan.png" />
- <TextBlock HorizontalAlignment="Left" Margin="171,656,0,97" Name="textBlock1" Text="Loading..." Width="208" Foreground="Black" FontSize="30" />
- </Grid>
- </UserControl>
- 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;
- namespace ProgressSplashScreen
- {
- public partial class PopupSplash : UserControl
- {
- public PopupSplash()
- {
- InitializeComponent();
- this.progressBar1.IsIndeterminate = true;//指示進度條是使用重復模式報告一般進度
- }
- }
- }
MainPage.xaml.cs
- 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 System.Windows.Controls.Primitives;
- using System.ComponentModel;
- using System.Threading;
- namespace ProgressSplashScreen
- {
- public partial class MainPage : PhoneApplicationPage
- {
- private Popup popup;
- private BackgroundWorker backroungWorker;
- public MainPage()
- {
- InitializeComponent();
- ShowPopup();
- }
- private void ShowPopup()
- {
- this.popup = new Popup();
- this.popup.Child = new PopupSplash();//設置 Popup 控件的內容,把自定義的PopupSplash控件填充到popup控件上
- this.popup.IsOpen = true;
- StartLoadingData();//開始加載數據
- }
- private void StartLoadingData()
- {
- //使用BackgroundWorker在單獨的線程上執行操作
- backroungWorker = new BackgroundWorker();
- //調用 RunWorkerAsync后臺操作時引發此事件,即后臺要處理的事情寫在這個事件里面
- backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork);
- //當后臺操作完成事件
- backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);
- //開始執行后臺操作
- backroungWorker.RunWorkerAsync();
- }
- //后臺操作完成
- void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- this.Dispatcher.BeginInvoke(() =>
- {
- this.popup.IsOpen = false;//關閉popup 注意要使用Dispatcher.BeginInvoke開跟UI通訊
- }
- );
- }
- //后臺操作處理
- void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- // 程序初始化處理 這里只是模擬了一下
- Thread.Sleep(7000);
- }
- }
- }
System.Windows.Controls.Primitives.Popup類
- <Popup>
- Child
- </Popup>
Popup控件可在單獨窗口中相對于屏幕上的元素或點顯示內容。當Popup可見時,IsOpen屬性設置為true。
Popup.Child屬性
內容模型:Child屬性是Popup控件的***內容屬性。一個 Popup 只能有一個 UIElement 作為子級,但該子級可以包含復雜的嵌入內容。例如,該子級可以是包含 Image、文本和其他類型控件的 StackPanel。 當將內容添加到 Popup 控件時,Popup 控件會成為該內容的邏輯父級。同樣,Popup 內容將被視為 Popup 的邏輯子級。不會將子內容添加到包含 Popup 控件的可視化樹中。但當 IsOpen 設置為 true 時,子內容將呈現在具有自身可視化樹的單獨的窗口中。
System.ComponentModel. BackgroundWorker類
BackgroundWorker 類允許您在單獨的專用線程上運行操作。耗時的操作(如下載和數據庫事務)在長時間運行時可能會導致用戶界面 (UI) 似乎處于停止響應狀態。如果您需要能進行響應的用戶界面,而且面臨與這類操作相關的長時間延遲,則可以使用 BackgroundWorker 類方便地解決問題。
若要在后臺執行耗時的操作,請創建一個 BackgroundWorker,偵聽那些報告操作進度并在操作完成時發出信號的事件。若要設置后臺操作,請為 DoWork 事件添加一個事件處理程序。在此事件處理程序中調用耗時的操作。若要啟動該操作,請調用 RunWorkerAsync。若要收到進度更新通知,請對 ProgressChanged 事件進行處理。若要在操作完成時收到通知,請對 RunWorkerCompleted 事件進行處理。
注意
您必須非常小心,確保在 DoWork 事件處理程序中不操作任何用戶界面對象。而應該通過 ProgressChanged 和 RunWorkerCompleted 事件與用戶界面進行通信。
BackgroundWorker 事件不跨 AppDomain 邊界進行封送處理。請不要使用 BackgroundWorker 組件在多個 AppDomain 中執行多線程操作。
如果后臺操作需要參數,請在調用 RunWorkerAsync 時給出參數。在 DoWork 事件處理程序內部,可以從 DoWorkEventArgs.Argument 屬性中提取該參數。
原文鏈接:http://www.cnblogs.com/linzheng/archive/2011/03/08/1977718.html
【編輯推薦】