1. the WPF Application is managed by the System. Windows. Application class.
2. Create a WPF Application
There are two ways to create a WPF application:
1. the default method of Visual Studio and Expression Blend. Use the App. xaml file to define how to start the application.
Program
The content of the App. xaml file is roughly as follows:
1: <Application x: Class = "WpfApplicationLifeCycle. App"
2: xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presen
Tation"
3: xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
4: StartupUri = "Window1.xaml">
5: <Application. Resources>
6: </Application. Resources>
7: </Application>
StartupUri specifies the start of the WPF form.
2. You can customize classes and define the Main method to start the WPF application.
Add a class to the project. The code of the class is as follows. In the project options, set this class as the startup Item.
1: using System;
2: using System. Windows;
3:
4: namespace WpfApplicationLifeCycle
5 :{
6: public class MainClass
7 :{
8: [STAThread]
9: static void Main ()
10 :{
11: // define the Application Object
12: Application app = new Application ();
13:
14: // Method 1: Call the Run method. The parameter is the start form object.
15: Window2 win = new Window2 ();
16: app. Run (win );
17:
18: // Method 2: Specify the MainWindow attribute of the Application object as the startup form,
Call the Run method without Parameters
19: // Window2 win = new Window2 ();
20: // app. MainWindow = win;
21: // win. Show (); // win. Show () is required here; otherwise, no
Display form
22: // app. Run ();
23:
24: // method 3:
25: // app. StartupUri = new Uri ("Window2.xaml", UriKind. R
Elative );
26: // app. Run ();
27 :}
28 :}
29 :}
3. Close the application
The policy for closing an application is specified by the ShutdownMode attribute and its type is System. Window.
S. ShutdownMode Enumeration type, which has the following enumerated members:
OnLastWindowClose default value): When the last form in the application is closed or Applic is called
The application closes when the Shutdown () method of the ation object;
OnMainWindowClose: when the main form is the start form) is closed or the S of the Application object is called
The application is closed when the hudown () method is used. Similar to the close mode of C # Windows applications );
OnExplicitShutdown: only when the Application object's Shutdown () method is called
The program will be closed;
You can change it directly in App. xaml:
1: <Application x: Class = "WpfApplicationLifeCycle. App"
2: xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presen
Tation"
3: xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
4: StartupUri = "Window1.xaml"
5: ShutdownMode = "OnExplicitShutdown">
6: <Application. Resources>
7: </Application. Resources>
8: </Application>
You can also change it in the code file App. xaml. cs.
1: Application app = new Application ();
2: Window2 win = new Window2 ();
3:
4: // to change the close mode, you must call the app. Run () method before
5: app. ShutdownMode = ShutdownMode. OnExplicitShutdown;
6: app. Run (win );
4. Application Object events
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/193T1FL-0.jpg "title =" qq 30918105806.jpg "alt =" 104050391.jpg"/>
The event processing of the application can be:
1. Bind events in App. xaml and add event handling methods to the App. xaml. cs file.
In the App. xaml file:
1: <Application x: Class = "WpfApplicationLifeCycle. App"
2: xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presen
Tation"
3: xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
4: StartupUri = "Window1.xaml"
5: Startup = "Application_Startup">
6: <Application. Resources>
7: </Application. Resources>
8: </Application>
In the App. xaml. cs file:
1: using System. Windows;
2:
3: namespace WpfApplicationLifeCycle
4 :{
5: // <summary>
6: // Interaction logic for App. xaml
7: // </summary>
8: public partial class App: Application
9 :{
10: private void Application_Startup (object sender, Startup
EventArgs e)
11 :{
12: // define the content to be processed when the application starts
13 :}
14 :}
15 :}
2. Normal C # event binding can be performed in the Custom class:
1: [STAThread]
2: static void Main ()
3 :{
4: // define the Application Object
5: Application app = new Application ();
6: Window2 win = new Window2 ();
7:
8: // Add event binding
9: app. Startup + = new StartupEventHandler (app_Startup );
10:
11: app. Run (win );
12 :}
13:
14: static void app_Startup (object sender, StartupEventArgs e)
15 :{
16: Window2 win = new Window2 ();
Win. Show ();
Win. button1.Content = "YOU! ";
17 :}
When loading the main form, YOU will be displayed on the button1 defined in Window2!
V. WPF Application Lifecycle
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/193T12060-1.jpg "title =" qq 30918110846.jpg "alt =" 104211690.jpg"/>
This article from the "51 records" blog, please be sure to keep this source http://kyirsheng.blog.51cto.com/3432734/1299964