Original: WPF Introductory Tutorial Series two--application Introduction
I. Application INTRODUCTION
WPF and WinForm are similar, and WPF has a Application object like WinForm to perform some global behavior and operations, and only one application instance exists in each domain (application domain). Unlike WinForm , WPF application is made up of two parts by default : App.xaml and App.xaml.cs, which is somewhat similar to ASP. NET WebForm, separates the definition from the behavior code.
Microsoft encapsulates the functionality that is often used in WPF in the application class . Application class specifically has the following functions :
- Track and interact with the lifetime of your application.
- Retrieves and processes command-line arguments.
- Detects and responds to unhandled exceptions.
- Share application-scoped properties and resources.
- Manage windows in stand-alone applications.
- Track and manage navigation.
Two. Launch of a WPF application
about how to create a "WPF Application" in Visual Studio, as already mentioned in the previous article. See WPF Introductory Tutorial Series one-basics.
1. Create a WPF application in Visual Studio 2013 and use the App.xaml file definition to launch the application. XAML is strictly not a purely XML format file, it is more like a DSL (domain specific Language, domain-specific language), and all its definitions are compiled by the compiler into code at the end. App.xaml file default content such as:
2, of course, if you get used to writing the boot in the code. You can also define a main method in a class that implements the launch of a WPF application, as in WinForm.
The first step: for example, in Solution Explorer, use the left mouse button to select the App.xaml file, then the right mouse button, pop-up "pop-up menu-" Excluded from the project ", so that in the project removed the App.xaml file we just created.
Step Two: Now add a new class with the class name App.cs. Such as. In Solution Explorer, use the left mouse button to select the "WpfApp1" item and then right-click to pop Up "pop-up Menu", as shown in.
Step three: In the "Add New Item" screen that pops up, select "Class" and change the name to "App.cs". Such as.
Fourth step: In Solution Explorer, double-click App.cs (such as), open the file, and then write the following three ways to debug the startup effect, respectively.
The first code to start the application
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;namespacewpfapp1{classApp {[STAThread]Static voidMain () {//defining the Application object as the entire application portalApplication App=Newapplication (); //method One: Call the Run method, which is the same way as the WinForm callWindowgrid Win=NewWindowgrid (); App. Run (Win); } }}
The second code to start the application
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;namespacewpfapp1{classApp {[STAThread]Static voidMain () {//defining the Application object as the entire application portalApplication App=Newapplication (); //Specify the MainWindow property of the Application object as the startup form, and then call the Run method without parametersWindowgrid Win=NewWindowgrid (); App. MainWindow=win; //is required, otherwise the form cannot be displayedwin. Show (); App. Run (); } } }
The third code to start the application
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;namespacewpfapp1{classApp {[STAThread]Static voidMain () {//defining the Application object as the entire application portalApplication App=Newapplication (); //start with a URLapp. StartupUri=NewUri ("Windowgrid.xaml", urikind.relative); App. Run (); } }}
3. The above method, the final execution of the effect, such as.
WPF Getting Started Tutorial series two--application Introduction