首页 > 程序开发 > 移动开发 > Windows Phone >

初学Windows Phone:弹出式(popup)页面加截状态提示

2012-03-17

这个示例主要是把popup封装成一个能用的自定义类库,当然在功能上还有很多不足,抛砖引玉吧。1.新建一个Windows Phone项目(PhoneApp1)。2.在解决方案中再新建一个Windows Phone的类库项目(CusPopup)。3.Phon...

这个示例主要是把popup封装成一个能用的自定义类库,当然在功能上还有很多不足,抛砖引玉吧。

1.新建一个Windows Phone项目(PhoneApp1)。
2.在解决方案中再新建一个Windows Phone的类库项目(CusPopup)。
3.PhoneApp1引用CusPopup。
4.在CusPopup中新建一个用户自定义控件(PopupPannel.xaml)。
5. 在CusPopup中新建一个类文件(PopupMain.cs)。
注:PhoneApp1用来做示例演示,CusPopup是我们要做功能的主要部分,其中PopupPannel.xaml的作用是弹出层的内容,赋给Popup.Child,这里的内容很简单我只放了一个TextBlock用于显示进度;PopupMain.cs是主要功能类,它的作用是显示/隐藏PopupPannel的内容,开启一个后台进程,并做一些其它事情。
6.在PopupPannel.xaml页中放入一个TextBlock控件,位置和样式根据需要调整。
7.PopupMain.cs的代码如下:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.ComponentModel;
6 using System.Windows.Controls.Primitives;
7 namespace CusPopup
8 {
9 public class PopupMain
10 {
11 private Popup popup;
12 private BackgroundWorker backgroundWorker;
13 private Action<object, DoWorkEventArgs> dowork;
14 private Action<object, RunWorkerCompletedEventArgs> runWorkerCompleted;
15 private Action<object, ProgressChangedEventArgs> progressChanged;
16 /// <summary>
17 /// 打开进度状态显示
18 /// </summary>
19 public void OpenPopup()
20 {
21 if (popup == null)
22 popup = new Popup();
23 popup.Child = new CusPopup.PopupPannel();
24 popup.IsOpen = true;
25
26 }
27 /// <summary>
28 /// 关闭进度状态显示
29 /// </summary>
30 public void ClosePopup()
31 {
32 popup.IsOpen = false;
33 }
34 /// <summary>
35 /// 进度改变后把信息显示到页面上
36 /// </summary>
37 /// <param name="ProgressPercentage"></param>
38 public void BackgroundWorkerProgressChanged(int ProgressPercentage)
39 {
40 CusPopup.PopupPannel cuspopup = popup.Child as CusPopup.PopupPannel;
41 cuspopup.textBlock2.Text = string.Format("{0}%", ProgressPercentage);
42
43 }
44 /// <summary>
45 /// 开启一个BackgroundWorker
46 /// </summary>
47 /// <param name="Dowork"></param>
48 /// <param name="RunWorkerCompleted"></param>
49 /// <param name="ProgressChanged"></param>
50 /// <param name="obj"></param>
51 public void RunBackgroundWorker(Action<object,DoWorkEventArgs> Dowork, Action<object, RunWorkerCompletedEventArgs> RunWorkerCompleted, Action<object, ProgressChangedEventArgs> ProgressChanged,object obj=null)
52 {
53 dowork = Dowork;
54 runWorkerCompleted = RunWorkerCompleted;
55 progressChanged = ProgressChanged;
56 backgroundWorker = new BackgroundWorker();
57 backgroundWorker.WorkerReportsProgress = true;
58 backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
59 backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted );
60 backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
61 backgroundWorker.RunWorkerAsync(obj);
62 }
63
64 void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
65 {
66 progressChanged(sender, e);
67 }
68
69 void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
70 {
71 if (runWorkerCompleted != null)
72 runWorkerCompleted(sender,e);
73 }
74
75 void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
76 {
77 if (dowork != null)
78 dowork(sender, e);
79
80 }
81 }
82 }



8.在PhoneApp1中的MainPage.xaml.cs中写如下代码:

View Code
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.Threading;
14 using System.ComponentModel;
15 using System.Windows.Controls.Primitives;
16 namespace PhoneApp1
17 {
18 public partial class MainPage : PhoneApplicationPage
19 {
20 CusPopup.PopupMain popupMain;//定义一个对象
21 public MainPage()
22 {
23 InitializeComponent();
24 popupMain = new CusPopup.PopupMain();//初始化
25 showPopup();
26 }
27 /// <summary>
28 /// 执行
29 /// </summary>
30 private void showPopup()
31 {
32 popupMain.OpenPopup();//打开层
33 //开启一个后台进程,并做一些其它的
34 popupMain.RunBackgroundWorker(
35 (obj, doWorkEventArgs) =>
36 {
37 DoWork(obj, doWorkEventArgs);
38 },
39 (obj, runWorkerCompletedEventArgs) =>
40 {
41 RunWorkerCompleted(obj, runWorkerCompletedEventArgs);
42 },
43 (obj, progressChangedEventArgs) =>
44 {
45 ProgressChanged(obj, progressChangedEventArgs);
46 },
47 null);
48
49 }
50
51
52 void ProgressChanged(object sender, ProgressChangedEventArgs e)
53 {
54 popupMain.BackgroundWorkerProgressChanged(e.ProgressPercentage);
55 }
56
57 void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
58 {
59 //Do Something
60 popupMain.ClosePopup();
61 }
62
63 void DoWork(object sender, DoWorkEventArgs e)
64 {
65 BackgroundWorker worker = sender as BackgroundWorker;
66 for (int i = 0; i < 100; i++)
67 {
68 worker.ReportProgress(i + 1);
69 Thread.Sleep(100);
70 }
71 }
72 }
73 }


在这里,DoWork我只是做了一个循环延时,然后把进度报告给BackgroundWorker,并执行BackgroundWorkerProgressChanged使界面上的文字发生变化显示。RunWorkerCompleted完成后关闭状态进度。方法的最后一个参数为null,如果DoWork需要参数可以从这里传入。

源码下载:http://up.2cto.com/2012/0317/20120317101546799.rar

摘自 坏猫居
相关文章
最新文章
热点推荐