Blog Park WPF Application class

Source: Internet
Author: User

All WPF programs are created by an instance of the application class that is responsible for opening the current application and form, and deciding how to close the application. The main functions of the application class can be summarized briefly as follows:

1. Keep the main thread

With application, if a form does not exit, the application does not exit, he will redefine the new main form, and only if all the forms are closed, Appliction decides whether to exit the application. Application The role of managing all forms. Look at the following code:

class program{     [STAThread ()]     staticvoid Main (string[] args)     {          New  application ();          New Window ();         App. Run (Win);}     }

The effect opens an application, and if we do not use application, the code is as follows:

class program{     [STAThread ()]     staticvoid Main (string[] args)     {         New  Window ();         Win. ShowDialog ();     }}

Feel the effect, there is no difference, but in fact, two is completely different, if we define a MainWindow class, put a button in this class, pop another form, the effect will be what, see the following code:

classProgram {[STAThread ()]Static voidMain (string[] args) {Application App=Newapplication (); Window win=NewWindow (); Win. MouseDown+=Win_mousedown; Win.           ShowDialog (); App.       Run (); }      Private Static voidWin_mousedown (Objectsender, System.Windows.Input.MouseButtonEventArgs e) {Window win=NewWindow (); Win.       Show (); }   }

when we close the parent form, the application does not exit. If application is removed, write the following code:

classProgram {[STAThread ()]Static voidMain (string[] args) {Window win=NewWindow (); Win. MouseDown+=Win_mousedown; Win.               ShowDialog (); }       Private Static voidWin_mousedown (Objectsender, System.Windows.Input.MouseButtonEventArgs e) {Window win=NewWindow (); Win.        Show (); }    }

When we close the parent form, the entire application exits.

This means that Applicationn can guarantee the continuous operation of the software in the presence of any form, ensuring that the main thread does not exit.

2. Start a XAML form

We can tell appliction how to start the form by specifying StartupUri, whose code is as follows:

class program{     [STAThread ()]     staticvoid Main (string[] args)     {          New application ();          New Uri ("mainwindow.xaml", System.UriKind.Relative);         App. Run ();}     }

So, when we write a WPF application, there is a App.xaml file, which is the definition of appliction, to see its code as follows:

<application x:class="Ani.app"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="Clr-namespace:ani"StartupUri="MainWindow.xaml"></Application>

Also by specifying the StartupUri property, start the form, that application how to start it, we can in the current program's obj directory, you can see the App.g.cs file, open the code inside, you can see the following code:

 Public Partial classApp:System.Windows.Application {/// <summary>     ///InitializeComponent/// </summary>[System.Diagnostics.DebuggerNonUserCodeAttribute ()] [System.CodeDom.Compiler.GeneratedCodeAttribute ("PresentationBuildTasks","4.0.0.0")]      Public voidInitializeComponent () {#line5 ". \.. \app.xaml " This. StartupUri =NewSystem.Uri ("MainWindow.xaml", System.UriKind.Relative); #lineDefault#lineHidden     }     /// <summary>     ///application Entry point. /// </summary>[ System.STAThreadAttribute ()] [System.Diagnostics.DebuggerNonUserCodeAttribute ()] [System.CodeDom.Compile R.generatedcodeattribute ("PresentationBuildTasks","4.0.0.0")]      Public Static voidMain () {Ani.app App=NewAni.app (); App.         InitializeComponent (); App.     Run (); }}

So the WPF application will take the initiative to create a main program App.cs and a App.xaml file, but App.cs does not release the editor, we just need to edit the App.xaml file to

3. How the application is closed

Application has a Shudonwmodel property, which is an enumeration object that specifies how the current application is closed with three options.

Onlastwindowclos E The default behavior, as long as at least one form exists, the application remains running, and the application automatically assigns the main form according to the form's opening sequence.
Onmainwindowclose This means that the application remains in operation as long as the main form (the latest open) is present
Onexplicaitshutdown The application does not end unless the Appliction.shutdown () method is called.

The code is as follows:

<application x:class="Ani.app"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="Clr-namespace:ani"StartupUri="MainWindow.xaml"Shutdownmode="Onlastwindowclose"></Application>

4. Application Events

Application provides a few events, but these events are very useful.

4.1 Startup

This event occurs before the Appliction.run () method is called and before the main form is displayed, and can be used to read the command-line arguments of the current application, and also to specify the StartupUri property using the event

4.2 Exit

The event occurs when the application shuts down (for whatever reason) and occurs before the run () method returns, but it cannot be canceled at this time, but the program can be restarted by the run () method.

4.3 sessionending

This event occurs at the end of the window conversation, for example, when the user shuts down or logs off the computer, we determine whether to close the application, and we can set the Sessionendingeventargscancel property to True to cancel closing the application. Otherwise, when event processing finishes, WPF calls the Application.shutdown () method to close the event.

4.4 Activated

Call when activating the current application, when switching from another application to the current application, when the form is first displayed

4.5 deactived

This event is triggered when the form in the application is deactivated. When you switch to another application, the event is called.

4.6 dispatcherunhandledexception

This event occurs whenever an unhandled exception occurs anywhere in the application, using the event to log important errors.

5. Handling Command Line Parameters

In order to handle command line arguments, you need to respond to the Application.Startup event. Command-line arguments are provided with parameters through the Startupeventargs.args property as a string array.

 public  partial  Span style= "Color:rgb (0, 0, 255); >class   app:application{  Private  object       sender, StartupEventArgs e) { if  (e.args.length > 0   "{ foreach  (
   
    in  
     E.args) {             Console.WriteLine (ARG); }         }     }}
   

6, responsible for the interaction between the forms

We can get the current appliction through the application, or we can get the current main form through the Application MainWindow property, so The data in the current application can be accessed through application, while the data in the main form can be accessed. We can define some of the global information in application, then the application can be accessed anywhere, implementing all forms interacting with each other, as in the following code:

 Public Partial classapp:application{ Publiclist<string> Globals =Newlist<string>(); Private voidApplication_Startup (Objectsender, StartupEventArgs e) {(APP) application.current). Globals.add ("Hello"); }}

Blog Park WPF Application class

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.