ProgramThere is usually a waiting process to start. In this process, you can use the popup control with the backgroundworker class to start the background thread.
ControlCode
Popupsplash. XAML
< Usercontrol X: Class = "Progresssplashscreen. popupsplash"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: d = "Http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"
MC: ignorable = "D"
Fontfamily =" {Staticresource phonefontfamilynormal} "
Fontsize =" {Staticresource phonefontsizenormal} "
Foreground =" {Staticresource phoneforegroundbrush} "
D: designheight = "800" D: designwidth = "480" >
< Grid X: Name = "Layoutroot" Background = "White" Width = "480" Height = "800" >
< Progressbar Horizontalalignment = "Left" Margin = "0,755" Name = "Progressbar1" Width = "480" Background = "Darkred" />
< Image Height = "757" Horizontalalignment = "Left" Name = "Image1" Stretch = "Fill" Verticalalignment = "TOP" Width = "480" Source = "/Progresssplashscreen; component/wuyuan.png" />
< Textblock Horizontalalignment = "Left" Margin = "171,656" Name = "Textblock1" Text = "Loading ..." Width = "208" Foreground = "Black" Fontsize = "30" />
</ Grid >
</ Usercontrol >
CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Namespace Progresssplashscreen
{
Public Partial Class Popupsplash: usercontrol
{
Public Popupsplash ()
{
Initializecomponent ();
This . Progressbar1.isindeterminate = True ; // Indicates that the progress bar is to report the general progress using repeated mode
}
}
}
Mainpage. XAML. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Using Microsoft. Phone. controls;
Using System. Windows. Controls. primitives;
Using System. componentmodel;
Using System. Threading;
Namespace Progresssplashscreen
{
Public Partial Class Mainpage: phoneapplicationpage
{
Private Popup popup;
Private Backgroundworker backroungworker;
Public Mainpage ()
{
Initializecomponent ();
Showpopup ();
}
Private Void Showpopup ()
{
This . Popup = New Popup ();
This . Popup. Child = New Popupsplash (); // Set the content of the popup control and add the custom popupsplash control to the popup control.
This . Popup. isopen = True ;
Startloadingdata (); // Start loading data
}
Private Void Startloadingdata ()
{
// Use backgroundworker to execute operations on a separate thread
Backroungworker = New Backgroundworker ();
// This event is triggered when the runworkerasync background operation is called, that is, the things to be processed in the background are written in this event.
Backroungworker. dowork + = New Doworkeventhandler (backroungworker_dowork );
// When the background operation is completed
Backroungworker. runworkercompleted + = New Runworkercompletedeventhandler (backroungworker_runworkercompleted );
// Start background operations
Backroungworker. runworkerasync ();
}
// Background operation completed
Void Backroungworker_runworkercompleted ( Object Sender, runworkercompletedeventargs E)
{
This . Dispatcher. begininvoke (() =>
{
This . Popup. isopen = False ; // Disable Popup. Use dispatcher. begininvoke to communicate with the UI.
}
);
}
// Background Operation Processing
Void Backroungworker_dowork ( Object Sender, doworkeventargs E)
{
// The initialization process is simulated here.
Thread. Sleep ( 7000 );
}
}
}
System. Windows. Controls. primitives. Popup class
XAML syntax
<Popup>
Child
</Popup>
The popup control displays content relative to the elements or dots on the screen in a separate window. When popup is visible, the isopen attribute is set to true.
Popup. Child attributes
Content Model: The child property is the only content property of the popup control. A popup can have only one uielement as a sub-level, but this sub-level can contain complex embedded content. For example, this sublevel can be a stackpanel that contains image, text, and other types of controls. When the content is added to the popup control, the popup control becomes the logical parent level of the content. Similarly, the popup content is considered as the logical sub-level of the popup. Child content is not added to the visualization Tree Containing the popup control. However, when isopen is set to true, the sub-content is displayed in a separate window with its own visualization tree.
System. componentmodel. backgroundworker class
The backgroundworker class allows you to run operations on individual dedicated threads. Time-consuming operations (such as downloads and database transactions) may cause the user interface (UI) to stop responding after a long time of running. You can use the backgroundworker class to solve the problem easily if you need a user interface that can respond and face long delays related to such operations.
To perform time-consuming operations in the background, create a backgroundworker to listen on events that report the operation progress and send signals when the operation is complete. To set background operations, add an event handler for the dowork event. This event handler calls time-consuming operations. To start this operation, call runworkerasync. To receive a progress update notification, handle the progresschanged event. To receive a notification when the operation is complete, handle the runworkercompleted event.
Note:
Make sure that you do not operate any user interface objects in the dowork event handler. You should use the progresschanged and runworkercompleted events to communicate with the user interface.
Backgroundworker events are not sent across appdomain boundaries. Do not use the backgroundworker component to perform multi-threaded operations in multiple AppDomains.
If the backend operation requires parameters, provide the parameters when calling runworkerasync. In the dowork event handler, you can extract this parameter from the doworkeventargs. Argument attribute.