当前位置:编程学习 > wap >>

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

这个示例主要是把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.

补充:移动开发 , Windows Phone ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,