Windows Phone 7程序等待頁面的處理
程序啟動通常會有一個等待的過程,在這個過程中可以通過使用Popup控件配合BackgroundWorker類啟動后臺線程來實現(xiàn)。
控件的代碼
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;//指示進度條是使用重復(fù)模式報告一般進度
- }
- }
- }
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();//設(shè)置 Popup 控件的內(nèi)容,把自定義的PopupSplash控件填充到popup控件上
- this.popup.IsOpen = true;
- StartLoadingData();//開始加載數(shù)據(jù)
- }
- private void StartLoadingData()
- {
- //使用BackgroundWorker在單獨的線程上執(zhí)行操作
- backroungWorker = new BackgroundWorker();
- //調(diào)用 RunWorkerAsync后臺操作時引發(fā)此事件,即后臺要處理的事情寫在這個事件里面
- backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork);
- //當后臺操作完成事件
- backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);
- //開始執(zhí)行后臺操作
- backroungWorker.RunWorkerAsync();
- }
- //后臺操作完成
- void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- this.Dispatcher.BeginInvoke(() =>
- {
- this.popup.IsOpen = false;//關(guān)閉popup 注意要使用Dispatcher.BeginInvoke開跟UI通訊
- }
- );
- }
- //后臺操作處理
- void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
- {
- // 程序初始化處理 這里只是模擬了一下
- Thread.Sleep(7000);
- }
- }
- }
System.Windows.Controls.Primitives.Popup類
- <Popup>
- Child
- </Popup>
Popup控件可在單獨窗口中相對于屏幕上的元素或點顯示內(nèi)容。當Popup可見時,IsOpen屬性設(shè)置為true。
Popup.Child屬性
內(nèi)容模型:Child屬性是Popup控件的***內(nèi)容屬性。一個 Popup 只能有一個 UIElement 作為子級,但該子級可以包含復(fù)雜的嵌入內(nèi)容。例如,該子級可以是包含 Image、文本和其他類型控件的 StackPanel。 當將內(nèi)容添加到 Popup 控件時,Popup 控件會成為該內(nèi)容的邏輯父級。同樣,Popup 內(nèi)容將被視為 Popup 的邏輯子級。不會將子內(nèi)容添加到包含 Popup 控件的可視化樹中。但當 IsOpen 設(shè)置為 true 時,子內(nèi)容將呈現(xiàn)在具有自身可視化樹的單獨的窗口中。
System.ComponentModel. BackgroundWorker類
BackgroundWorker 類允許您在單獨的專用線程上運行操作。耗時的操作(如下載和數(shù)據(jù)庫事務(wù))在長時間運行時可能會導(dǎo)致用戶界面 (UI) 似乎處于停止響應(yīng)狀態(tài)。如果您需要能進行響應(yīng)的用戶界面,而且面臨與這類操作相關(guān)的長時間延遲,則可以使用 BackgroundWorker 類方便地解決問題。
若要在后臺執(zhí)行耗時的操作,請創(chuàng)建一個 BackgroundWorker,偵聽那些報告操作進度并在操作完成時發(fā)出信號的事件。若要設(shè)置后臺操作,請為 DoWork 事件添加一個事件處理程序。在此事件處理程序中調(diào)用耗時的操作。若要啟動該操作,請調(diào)用 RunWorkerAsync。若要收到進度更新通知,請對 ProgressChanged 事件進行處理。若要在操作完成時收到通知,請對 RunWorkerCompleted 事件進行處理。
注意
您必須非常小心,確保在 DoWork 事件處理程序中不操作任何用戶界面對象。而應(yīng)該通過 ProgressChanged 和 RunWorkerCompleted 事件與用戶界面進行通信。
BackgroundWorker 事件不跨 AppDomain 邊界進行封送處理。請不要使用 BackgroundWorker 組件在多個 AppDomain 中執(zhí)行多線程操作。
如果后臺操作需要參數(shù),請在調(diào)用 RunWorkerAsync 時給出參數(shù)。在 DoWork 事件處理程序內(nèi)部,可以從 DoWorkEventArgs.Argument 屬性中提取該參數(shù)。
原文鏈接:http://www.cnblogs.com/linzheng/archive/2011/03/08/1977718.html
【編輯推薦】