Windows Phone for beginners: pop-up (popup) page with a screenshot status prompt

Source: Internet
Author: User

In this example, popup is encapsulated into a usable custom class library. Of course, there are still many deficiencies in functions.

1. Create a Windows Phone Project (Phoneapp1).
2. Create another Windows Phone Class Library Project (Cuspopup).
3. phoneapp1 references cuspopup.
4. Create a new user-defined control (Popuppannel. XAML).
5. Create a new class file (Popupmain. CS).
Note: phoneapp1 is used for example demonstration,CuspopupIs the main part of our function, among which popuppannel. the role of XAML is to assign the content of the pop-up layer to popup. child, the content here is very simple. I only put a textblock to display the progress; popupmain. CS is the main function class. Its function is to display/hide the content of popuppannel, start a background process, and do other things.
6. Place a textblock control on the popuppannel. XAML page. Adjust the position and style as needed.
7. The popupmain. CS code is as follows:

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;
12 private backgroundworker;
13 private action <object, doworkeventargs> dowork;
14 private action <object, runworkercompletedeventargs> runworkercompleted;
15 private action <object, progresschangedeventargs> progresschanged;
16 /// <summary>
17 // display the progress
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 // close progress status display
29 /// </Summary>
30 public void closepopup ()
31 {
32 Popup. isopen = false;
33}
34 /// <summary>
35 // after the progress changes, the information is displayed on the page
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 // enable a 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. Write the following code in mainpage. XAML. CS in phoneapp1:

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; // defines an object
21 public mainpage ()
22 {
23 initializecomponent ();
24 popupmain = new cuspopup. popupmain (); // Initialization
25 showpopup ();
26}
27 /// <summary>
28 // run
29 /// </Summary>
30 private void showpopup ()
31 {
32 popupmain. openpopup (); // open the Layer
33 // start a background process and do some other
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}
Here, I just made a loop delay for dowork, reported the progress to backgroundworker, and executed backgroundworkerprogresschanged to display the text on the interface. After runworkercompleted is completed, the status is disabled. The last parameter of the method is null. If dowork requires a parameter, it can be passed in from here. Source code download
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.