Property:
Current gets the application object for the currently AppDomain
MainWindow Gets or sets the main form of the application. (Detailed below)
Resources Gets or sets a collection of application-scoped assets, such as styles and brushes.
Shutdownmode Gets or sets the condition that causes the shutdown method call to occur.
StartupUri Gets or sets the UI that is automatically displayed when the application starts.
Windows gets the instantiation window in the application.
If the Shutdownmode property of the Application object is set to onmainwindowclose, closing the main window causes the application to close.
In the application startup process, the following example shows how to instantiate the MainWindow in the code
public partial class app:application{ void App_startup (object sender, StartupEventArgs e) { MainWindow window = new MainWindow (); Window. Show (); }}
In WPF programs, Try/catch blocks are generally used for catching exceptions. Just like bugs in a program, it's hard to ensure that all exceptions in the program are captured through Try/catch. If the exception is not captured, light affects the user experience, which can cause data loss when it is critical. Application.dispatcherunhandledexception events and Appdomain.unhandledexception events are available in WPF, and by registering these two events, we can perform custom processing on an unhandled global exception set.
For each exception that is not handled by code running on the main UI thread, application throws a dispatcherunhandledexception. You need to set the Handled property to True when you are dealing with an unhandled exception from Dispatcherunhandledexception, and you do not want WPF to continue processing the exception. In layman's words, if you do not want the application to crash, we need to set the Handled property to true. However, not all exceptions are recoverable, and if the exception is FileNotFoundException, the program can continue to run after the exception is handled, and if the exception is stackoverflowexception, it cannot continue and will close.
UnhandledException is raised for any unhandled exceptions in any thread, and for any exceptions in the application domain. If the exception in the UI thread is not handled, UnhandledException is also raised. Starting with the. NET Framework 4, a corrupted process state exception will not raise the event, such as a stack overflow, or an access violation. Because by default, the common language runtime (CLR) does not export these exceptions to managed code and does not call Try/catch blocks for them.
In simple code:
1//<summary> 2//Application launch 3//</summary> 4//<param name= "Sender" >& Lt;/param> 5//<param name= "E" ></param> 6 private void Application_Startup (object sender, StartupEventArgs e) 7 {8 current.dispatcherunhandledexception + = app_ondispatcherunhandledexception; 9 AppDomain.CurrentDomain.UnhandledException + = currentdomain_unhandledexception;10}11 12// /<summary>13///UI thread throws global exception event handling///</SUMMARY>15//<param name= "Sender" >< ;/param>16//<param name= "E" ></param>17 private void App_ondispatcherunhandledexception (o Bject sender, Dispatcherunhandledexceptioneventargs e) {try20 {$ Log Helper.Instance.Logger.Error (e.exception, "UI thread Global Exception"); e.handled = true;23}24 C Atch (Exception ex) 25 {LogHelper.Instance.Logger.Error (ex, "Unrecoverable UI thread Global exception"), MessageBox.Show ("Application An unrecoverable exception will be exited! ")}29}30///<SUMMARY>32///non-UI thread throw global exception event handling///</summary& GT;34//<param name= "sender" ></param>35//<param name= "E" ></param>36 p rivate void Currentdomain_unhandledexception (object sender, UnhandledExceptionEventArgs e) PNs {try39 {exception var = e.exceptionobject as exception;41 if (exception! = NULL) 4 2 {LogHelper.Instance.Logger.Error (exception, "Non-UI thread Global Exception"); 44}45 }46 catch (Exception ex) LogHelper.Instance.Logger.Error (ex, "unrecoverable Non-UI thread Global Exception "); MessageBox.Show (" The application has an unrecoverable exception and will exit! ") "); 50}51}
WPF Application class introduction and WPF exception handling