Create an application Manually
1. Create an empty Project
2. Adding references
3. Add ManualApp.cs and add the following code
[STAThread] Public Static void Main () { new Window (); " Manually created application " ; Win. Show (); New application (); App. Run ();}
[STAThread] Single threading model
4. A console window will appear to execute the program, and you can modify the output Type of project
Creating an application from a wizard
The main function is implemented in \obj\debug\app.g.cs
The life cycle of an application
Application starts run, enters the message loop, constantly responds to and processes events
Application Portal
The application portal is the main function, and the two main types of object Application\window, the former being unique in the global, can be obtained through application.current.
[System.STAThreadAttribute ()][system.diagnostics.debuggernonusercodeattribute ()] [ System.CodeDom.Compiler.GeneratedCodeAttribute ("presentationbuildtasks"" 4.0.0.0")]publicstaticvoid Main () { New Alex_wpfappdemo04.app (); App. InitializeComponent (); App. Run ();}
Application start
The first response event is the startup
The following method is used by default in App.xaml
<application x:class="alex_wpfappdemo04.app" xmlns=" Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml " Startup="application_startup">
Minor changes, will be the first to enter the Application_Startup event processing method
<application x:class="alex_wpfappdemo04.app" xmlns=" Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml " Startup="application_startup">
Status of the application
There are two states of an application, and ACTIVED/DEACTIVED,WPF can handle both states through the Actived/deactived event.
<application x:class="Alex_wpfappdemo04.app"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"StartupUri="MainWindow.xaml"Activated="application_actived"Deactivated="application_deactived">
Exception for application
<application x:class="Alex_wpfappdemo04.app"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"StartupUri="MainWindow.xaml"dispatcherunhandledexception="application_dispatcherunhandledexception">
When an application exception occurs, the Dispatcherunhandledexception event is triggered and the event handling logic is implemented to prevent the program from crashing
Non-normal exit
When an operating system shuts down, restarts, logs off, and so on, the application accepts the Sessionending event and can organize the shutdown, restart, logoff, and other operations of the operating system by setting the Cancel property
<application x:class="Alex_wpfappdemo04.app"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"StartupUri="MainWindow.xaml"sessionending="application_sessionending">
Private voidApplication_sessionending (Objectsender, SessionEndingCancelEventArgs e) { stringmsg =string. Format ("{0}. End Session?", e.reasonsessionending); Messageboxresult result= MessageBox.Show (msg,"Session Ending", Messageboxbutton.yesno); if(Result = =messageboxresult.no) {e.cancel=true; }}
Normal exit
The program exits normally when you close the main application window, all windows, and the shutdown function is called.
These 3 cases are determined by the Shutdownmode attribute
//Summary://specifies how an application would shutdown. Used by the System.Windows.Application.ShutdownMode//Property . Public enumShutdownmode {//Summary://An application shuts if either the last window closes, or System.Windows.Application.Shutdown () //is called.Onlastwindowclose =0, // //Summary://An application shuts if either the main window closes, or System.Windows.Application.Shutdown ()
//is called.Onmainwindowclose =1, // //Summary://An application shuts if System.Windows.Application.Shutdown ()//is called.Onexplicitshutdown =2, }
The SHUTDOWN function has two forms, not passing any arguments, passing in int as ExitCode
To be continue ...
WPF Learning Path (vii) applications and Windows