Windows Phone 7程序等待页面的处理

移动开发
程序启动通常会有一个等待的过程,在这个过程中可以通过使用Popup控件配合BackgroundWorker类启动后台线程来实现。

程序启动通常会有一个等待的过程,在这个过程中可以通过使用Popup控件配合BackgroundWorker类启动后台线程来实现。

控件的代码

PopupSplash.xaml

  1. <UserControl x:Class="ProgressSplashScreen.PopupSplash" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     mc:Ignorable="d" 
  7.     FontFamily="{StaticResource PhoneFontFamilyNormal}" 
  8.     FontSize="{StaticResource PhoneFontSizeNormal}" 
  9.     Foreground="{StaticResource PhoneForegroundBrush}" 
  10.     d:DesignHeight="800" d:DesignWidth="480"> 
  11.       
  12.     <Grid x:Name="LayoutRoot" Background="White" Width="480" Height="800"> 
  13.         <ProgressBar HorizontalAlignment="Left" Margin="0,755,0,0" Name="progressBar1" Width="480" Background="DarkRed"   /> 
  14.         <Image Height="757" HorizontalAlignment="Left" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="480" Source="/ProgressSplashScreen;component/wuyuan.png" /> 
  15.         <TextBlock HorizontalAlignment="Left" Margin="171,656,0,97" Name="textBlock1" Text="Loading..." Width="208" Foreground="Black" FontSize="30" /> 
  16.     </Grid> 
  17. </UserControl> 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12.  
  13. namespace ProgressSplashScreen  
  14. {  
  15.     public partial class PopupSplash : UserControl  
  16.     {  
  17.         public PopupSplash()  
  18.         {  
  19.             InitializeComponent();  
  20.             this.progressBar1.IsIndeterminate = true;//指示进度条是使用重复模式报告一般进度  
  21.         }  
  22.     }  

MainPage.xaml.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. using Microsoft.Phone.Controls;  
  13. using System.Windows.Controls.Primitives;  
  14. using System.ComponentModel;  
  15. using System.Threading;  
  16.  
  17. namespace ProgressSplashScreen  
  18. {  
  19.     public partial class MainPage : PhoneApplicationPage  
  20.     {  
  21.         private Popup popup;  
  22.         private BackgroundWorker backroungWorker;  
  23.         public MainPage()  
  24.         {  
  25.             InitializeComponent();  
  26.             ShowPopup();  
  27.         }  
  28.         private void ShowPopup()  
  29.         {  
  30.             this.popup = new Popup();  
  31.             this.popup.Child = new PopupSplash();//设置 Popup 控件的内容,把自定义的PopupSplash控件填充到popup控件上  
  32.             this.popup.IsOpen = true;  
  33.             StartLoadingData();//开始加载数据  
  34.         }  
  35.  
  36.         private void StartLoadingData()  
  37.         {  
  38.             //使用BackgroundWorker在单独的线程上执行操作  
  39.             backroungWorker = new BackgroundWorker();  
  40.             //调用 RunWorkerAsync后台操作时引发此事件,即后台要处理的事情写在这个事件里面  
  41.             backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork);  
  42.             //当后台操作完成事件  
  43.             backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);  
  44.             //开始执行后台操作  
  45.             backroungWorker.RunWorkerAsync();  
  46.         }  
  47.         //后台操作完成  
  48.         void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
  49.         {  
  50.             this.Dispatcher.BeginInvoke(() => 
  51.                 {  
  52.                     this.popup.IsOpen = false;//关闭popup  注意要使用Dispatcher.BeginInvoke开跟UI通讯  
  53.                 }  
  54.             );  
  55.         }  
  56.         //后台操作处理  
  57.         void backroungWorker_DoWork(object sender, DoWorkEventArgs e)  
  58.         {  
  59.             // 程序初始化处理   这里只是模拟了一下  
  60.             Thread.Sleep(7000);  
  61.         }  
  62.     }  

System.Windows.Controls.Primitives.Popup类

  1. <Popup> 
  2.   Child  
  3. </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

【编辑推荐】

  1. 爱奇艺Windows Phone 7改版项目总结
  2. Windows Phone开发人员七宗罪
  3. 如何为Windows Phone开发App之构思和概念篇
  4. 如何将Windows Phone应用移植到Windows 8平台
  5. Windows Phone 7处理休眠和墓碑的恢复
责任编辑:王晓东 来源: 博客园
相关推荐

2012-06-07 09:33:13

Windows Pho

2010-11-03 15:10:04

SilverlightSilverlightWindows Pho

2011-03-21 09:05:40

IronRubyWindows Pho

2010-11-26 16:00:08

Windows Pho

2013-07-30 11:18:37

Windows PhoWindows Pho

2010-05-05 13:16:02

Windows PhoWindows CE

2010-08-30 14:53:11

微软Windows Pho

2010-12-01 09:01:31

独立存储Windows Pho

2012-06-01 15:39:46

休眠状态墓碑状态

2011-02-21 10:16:16

多任务处理功能Windows Pho

2010-05-11 16:47:32

Windows Pho

2013-04-17 10:54:18

Windows PhoWindows Pho

2011-06-07 11:35:38

Windows Pho

2010-08-05 15:26:19

Windows Pho

2010-10-25 14:07:55

Windows Pho

2012-08-16 10:35:50

Windows Pho

2010-07-21 14:56:21

Windows Pho

2011-06-08 09:43:15

Windows Pho

2010-12-01 13:40:13

枢轴控件Windows Pho

2010-12-14 18:48:49

微软
点赞
收藏

51CTO技术栈公众号