How to start a WPF Program

Source: Internet
Author: User

1. The first method is the default method, which is configured through startupuri = "mainwindow. XAML" in APP. XAML.

1 <Application x:Class="BaseElement.App"2                     xmlns=""3                     xmlns:x=""4                     StartupUri="MainWindow.xaml">5       <Application.Resources>6       </Application.Resources>7 </Application>
View code

2. Create an application object to start the program. Right-click the project and choose Properties> Application> startup object to set startup.

 1  public class Startup 2     { 3         [STAThread] 4         public static void Main() 5         { 6  7             // Create the application. 8             Application app = new Application(); 9             // Create the main window.10             MainWindow win = new MainWindow();11             // Launch the application and show the main window.12             app.Run(win);13         }14     }
View code

3. Modify the app. XAML. CS file. Add the main method. Delete the main method in the app. G. CS file at the same time

1 /// <summary> 2 /// app. interaction logic of XAML 3 // </Summary> 4 public partial class app: Application 5 {6 7 [stathread ()] 8 public static void main () 9 {10 baseelement. app = new baseelement. APP (); 11 app. initializecomponent (); 12 app. run (); 13} 14 public void initializecomponent () 15 {16 this. startupuri = new uri ("mainwindow. XAML ", system. urikind. relative); 17} 18}
View code

4. The fourth method is to modify the app. XAML. CS file, which is not the same as the third method. This method also includes the command-line arguments (command line parameter)

 1     public partial class App : Application 2     { 3         [STAThread] 4         public static void Main() 5         { 6             App app = new App(); 7             app.Run(); 8         } 9 10         public App()11         {12             this.Startup += new StartupEventHandler(App_Startup);13         }14 15         private static void App_Startup(object sender, StartupEventArgs e)16         {17             // Create, but don‘t show the main window.18             MainWindow win = new MainWindow();19             if (e.Args.Length > 0)20             {21                 string file = e.Args[0];22                 if (System.IO.File.Exists(file))23                 {24                     // Configure the main window.25                     win.LoadFile(file);26                 }27             }28             else29             {30                 // (Perform alternate initialization here when31                 // no command-line arguments are supplied.)32             }33             // This window will automatically be set as the Application.MainWindow.34             win.Show();35         }36     }
View code

I wrote my blog for the first time. If you have any questions, please include them. I just learned about WPF and I am a newbie!

How to start a WPF Program

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: 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.