Disclaimer: Anyone and organizations are welcome to repost the articles in this blog, but the original links and author information must be marked.
Link: http://blog.csdn.net/li_007/archive/2010/11/04/5987896.aspx
Pioneering little turtle -------> csdn
A WPF application must have an application object instance or its subclass instance. It represents a Windows Process of WPF and provides the life cycle of the WPF program, it also integrates the clickonce deployment function. Or, more specifically, in WPF, an application is an instance of the application class or its subclass in the system. Windows namespace.
An application is a class that encapsulates the unique functions of the WPF application, including:
Application Survival: activated, current, deactivated, dispatcherunhandledexception, exit, run, sessionending, shutdown, shutdownmode, and startup.
Application-range window, attributes, and resource management: findresource, getcontentstream, getresourcestream, loadcomponent, mainwindow, properties, resources, startupuri, and windows.
Command line parameters and exit code processing: application. startup, application. Exit, and application. shutdown.
Navigation: fragmentnavigation, loadcompleted, navigated, navigating, navigationprogress, navigationstopped, navigationfailed, setcookie, and getcookie.
Application implements a single instance mode to provide shared access to its windows, properties, and resource range services. Therefore, for each appdomain, only one instance of the application class can be created. You can use tags, tags, and code hiding or code to implement the application. If the application is implemented using a tag (whether it is a tag or code hidden), the Tag file must be configured as a Microsoft build engine (msbuild) applicationdefinition item.
Take a look at the following simple WPF instance code:
Using system; <br/> namespace simplewpfapp <br/> {<br/> class simplewpfapp <br/> {<br/> [stathread] <br/> Public static void main () <br/>{< br/> system. windows. window WND = new system. windows. window (); <br/> WND. show (); </P> <p> system. windows. application APP = new system. windows. application (); <br/> app. run (); <br/>}< br/>}
Note that you must not forget to add [stathread] before the main function. Otherwise, the compilation will pass, but the running will fail.
[Stathread]: A single threaded apartment (STA), as part of an unmanaged Component Object Model (COM), is used to manage calls between COM. Because many com applications are. if multiple threads are used in the. NET environment, the referenced com cannot run normally. If the program is not declared as stathread ,. net will automatically use multithreading to improve efficiency, which will lead to unpredictable consequences. All performance layer frameworks of MS, managed and unmanaged
, All must run in the initialized stathread.
Now let's talk about the complete application class or subclass connected by the. XAML file and the. Xmal. CS file (Code-behind) through the partial keyword compilation.
First, the. XAML File
<Application X: class = "helloworldwpf. apps "<br/> xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "<br/> xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "<br/> startupuri =" mainwindow. XAML "> <br/> <application. resources> </P> <p> </application. resources> <br/> </Application>
Below is the. XAML. CS File
Using system; <br/> using system. collections. generic; <br/> using system. configuration; <br/> using system. data; <br/> using system. LINQ; <br/> using system. windows; <br/> namespace helloworldwpf <br/> {<br/> /// <summary> <br/> // interaction logic for app. XAML <br/> /// </Summary> <br/> Public partial class app: application <br/>{< br/>}< br/>
When the compiler encounters the partial keyword, It will combine the. XAML and. XAML. CS files into a complete app class (subclass of the application ). When the program starts and runs, the following steps are displayed:
1. Create an application Class Object (actually a subclass object) and set its application. Current attribute.
2. Create and display the UI Based on The XAML file specified by the startupuri attribute.
3. Set the mainwindows attribute of the application Class Object (which is actually a subclass object of the application.
4. Call the run method of the application Class Object (which is actually a subclass object of the application) and keep the application running until the main window is closed.
Add two points:
A. You can also create and display a UI in response to the startup event. The Code photo is as follows:
B. Change the definition file of the application class and main function by setting the build action attribute of the XAML file to applicationdefinition.
Application events are an important part of the application and the foundation of the com4 programming language. The lifecycle of a standard application can be considered as a combination of events. Always start startup to exit.
|
Name |
Description |
|
Activated |
This occurs when the application becomes a foreground application. |
|
Deactivated |
This occurs when the application stops serving as the foreground application. |
|
Dispatcherunhandledexception |
An exception occurs when it is thrown by the application but not processed. |
|
Exit |
It happens just before the application is closed and cannot be canceled. |
|
|
Loadcompleted |
It occurs when the content that has been loaded, analyzed, and displayed in the application's navigator navigation. |
|
Sessionending |
This occurs when you end a windows session by logging out or disabling the operating system. |
|
Startup |
This occurs when the run method of the Application object is called. |
Use an instance to describe the process of these events:
<Application X: class = "helloworldwpf. apps "<br/> xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "<br/> xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "<br/> startupuri =" mainwindow. XAML "<br/> startup =" app_startup "<br/> activated =" app_activated "<br/> deactivated =" app_deactivated "<br/> dispatcherunhandledexception =" failed "<br /> exit = "app_exit"> <br/> <application. resources> </P> <p> </application. resources> <br/> </Application>
App. XAML
Using system; <br/> using system. collections. generic; <br/> using system. configuration; <br/> using system. data; <br/> using system. LINQ; <br/> using system. windows; <br/> using system. diagnostics; // For debug <br/> using system. windows. threading; // dispatcherunhandledexceptioneventargs <br/> namespace helloworldwpf <br/>{< br/> /// <summary> <br/> // interaction logic for app. XAML <br/> /// </Summary> <br/> Public partial class app: application <br/>{< br/> // startup event <br/> void app_startup (Object sender, startupeventargs ARGs) <br/>{< br/> debug. writeline ("Startup event"); <br/>}< br/> // activated event <br/> void app_activated (Object sender, eventargs ARGs) <br/>{< br/> debug. writeline ("activated event"); <br/>}< br/> // deactivated event <br/> void app_deactivated (Object sender, eventargs ARGs) <br/>{< br/> debug. writeline ("Deactivated event"); <br/> throw new exception ("dispatcher unhandled exception "); <br/>}< br/> // dispatcherunhandledexception event <br/> void app_dispatcherunhandledexception (Object sender, <br/> dispatcherunhandledexceptioneventargs ARGs) <br/> {<br/> // process unhandled exception <br/> //...... <br/> // <br/> debug. writeline ("dispatcherunhandledexception event"); <br/> // prevent default unhandled exception processing <br/> args. handled = true; <br/>}< br/> // exit event <br/> void app_exit (Object sender, exiteventargs ARGs) <br/>{< br/> debug. writeline ("Exit event"); <br/>}< br/>
App. Xmal. CS
The running result is as follows:
Result Analysis:
1. Run triggers the startup event.
2. Activated event triggered when the window is rendered with focus
3. I set the window to lose focus to trigger the deactivated event, but an unhandled exception is automatically thrown in the deactivated event processing function,
Trigger the dispatcherunhandledexception event.
4. Let the window get focus again to trigger the activated event
5. Close the window and cause the deactivated event to be triggered because the window has no focus. In this case, an unhandled exception is thrown and the dispatcherunhandledexception event is triggered.
6. Exit the program and trigger the exit event. The program ends its life cycle.
Okay. For other application services, such as clickonce, You can query msdn.