WPF getting started tutorial Series 2-Application Introduction, wpf getting started tutorial
1. Application Introduction
WPF and WinForm are very similar. WPF and WinForm have an Application object to perform some global behaviors and operations, and only one Application instance exists in each Domain (Application Domain. Unlike WinForm, WPF Application is composed of App. xaml and App. xaml. cs by default. This is a bit similar to Asp. Net WebForm, which separates definitions from behavior code.
Microsoft encapsulates all frequently used functions in wpf in the Application class. The Application class has the following functions:
- Tracks the lifetime of an application and interacts with it.
- Retrieves and processes command line parameters.
- Detects and responds to unprocessed exceptions.
- Share attributes and resources within the application scope.
- Manage windows in independent applications.
- Tracking and management navigation.
Ii. Start of the WPF Application
As mentioned in the previous article about how to create a "WPF Application" in Visual Studio. For more information, see Chapter 1 of the WPF getting started tutorial series.
1. Create a "WPF Application" in Visual Studio 2013 and start the application using the App. xaml file definition. In a strict sense, XAML is not a pure XML file. It is more like a DSL (Domain Specific Language, Domain-Specific Language ), all its definitions are finally compiled into code by the compiler. The default content of the App. xaml file is as follows:
2. Of course, if you are used to writing startup in the code. You can also define a Main method in the class as in WinForm to start the WPF application.
Step 1: for example, in Solution Explorer, click the left mouse button to select the App. right-click the xaml file and choose "pop-up menu> exclude from project" to remove the App we just created in the project. xaml file.
Step 2: Add a new class named App. cs. For example. In Solution Explorer, right-click the "WpfApp1" project and choose "pop-up menu", as shown in.
Step 3: on the "Add new project" Page, select "class" and change the name to "App. cs ". For example.
Step 4: In Solution Explorer, double-click App. cs (for example) to open the file. Then, you can write the following code to debug the startup effect.
First, start the application code.
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. windows; namespace WpfApp1 {class App {[STAThread] static void Main () {// define the Application object as the entire Application entry Application app = new Application (); // Method 1: call the Run method. This method is the same as winform's call. WindowGrid win = new WindowGrid (); app. run (win );}}}
Second, start the application code
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. windows; namespace WpfApp1 {class App {[STAThread] static void Main () {// define the Application object as the Application entry Application app = new Application (); // specify the MainWindow attribute of the Application object as the startup form, and then call the Run method WindowGrid win = new WindowGrid (); app. mainWindow = win; // required; otherwise, the window win cannot be displayed. show (); app. run ();}}}
Code for starting an application
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. windows; namespace WpfApp1 {class App {[STAThread] static void Main () {// define the Application object as the Application entry Application app = new Application (); // start the app by Url. startupUri = new Uri ("WindowGrid. xaml ", UriKind. relative); app. run ();}}}
3. The last execution result of the above method, as shown in.