WPF uses the Main function to start the program, wpfmain

Source: Internet
Author: User

WPF uses the Main function to start the program, wpfmain

WPF default program startup: Specify the program startup method (StartupUri = "MainWindow. xaml") in the App. xaml automatically generated after the project is created. The following code starts the MainWindow page:

<Application x: Class = "MyFirstWPFApplication. App"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: local = "clr-namespace: MyFirstWPFApplication"
StartupUri = "MainWindow. xaml">
<Application. Resources>

</Application. Resources>
</Application>

 

WPF uses the Main function to start the program: Write the Main function as the startup point.

1. Write the Main function in the automatically generated App. cs file of WPF.

Public partial class App: Application {[STAThread] static void Main (){
// Method 1 Application app = new Application (); MyWindow win = new MyWindow (); app. run (win); // method 2 MyWindow win = new MyWindow (); app. mainWindow = win; win. show (); app. run (); // method 3 app. startupUri = new Uri ("MyWindow. xaml ", UriKind. relative); app. run ();}}

2. Create a class and write the Main function.

Public static class StartUpClass
{

// [STAThread] attribute indicates that the COM thread model of the application is a single-threaded unit.
[STAThread]
Public static void Main (string [] args)
{
SingleInstanceStart = new SingleInstanceStart ();
SingleInstanceStart. Run (args );
}
}

Windows formsapplicationbase must reference Microsoft. VisualBasic. dll

Public class SingleInstanceStart: WindowsFormsApplicationBase
{
Protected override bool OnStartup (StartupEventArgs e)
{
// DoSomething
Return true;
}
}

Finally, you need to change the attributes of the App. xaml file.

The generation operation is changed from ApplicationDefinition to Page.

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.