WPF (1) Main Function, wpf

Source: Internet
Author: User

WPF (1) Main Function, wpf

The entry function of the application is the main function, which is clearly defined in both the Console program and the main function of the Winform program and can be easily found, however, the main function definition cannot be found in the WPF project file. Does WPF need the main function? NO! NO! NO! Don't be naive. WPF has the main function. VS automatically generates the function in obj \ Debug \ App. g. the following definition can be seen in the cs file (the obj \ Debug folder also contains an App. g. i. cs files, file content and App. g. cs consistency. This file is also automatically generated for VS's intelligence smart sensing service. I will discuss intelligence later ).

The Main function is defined in the App class. The App class is automatically generated by VS and inherits from System. windows. the Application class maintains the lifecycle of an Application. Each running WPF program is represented by an instance of the Application. The two key statements in the above Code are:

This. StartupUri = new System. Uri ("MainWindow. xaml", System. UriKind. Relative );

App. Run ();

These two statements start the application and use the window defined by "MainWindow. xaml" as the main window of the application. The automatically generated code is relatively simple. The Application class can do more than this. I will discuss it in detail later.

 

The above is the automatically generated Main function, you can also manually create our own Main function, as shown below:

Public class StartUp
{
[STAThread]
Public static void Main (string [] args)
{
Var app = new App
{
StartupUri = new System. Uri ("MainWindow. xaml", System. UriKind. Relative)
};
App. Run ();
}
}

However, you will get the following compilation error:

Error CS0017: Program has more than one entry point defined. Compile with/main to specify the type that contains the entry point.

The reason is also very simple. VS helps you generate a Main function. You have defined another Main function, but the application can only have one entry, and the system does not know which one to use, therefore, you must manually specify one. On the project property Application page, set the Startup object to the class StartUp we just defined.

 

Related Article

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.