欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-01-23 07:56:08
这个示例主要是把popup封装成一个能用的自定义类库,当然在功能上还有很多不足,抛砖引玉吧。 1.新建一个windows phone项目(phoneapp1)。 2.在解决方案中再新建一个windo...

这个示例主要是把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需要参数可以从这里传入。

 下载:
 

摘自  坏猫居