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.