代碼:
/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { public App() { Debug.WriteLine("App constructor"); this.Startup += new StartupEventHandler(App_Startup); } void App_Startup(object sender, StartupEventArgs e) { Debug.WriteLine("App_Startup"); MainWindow win = new MainWindow(); this.MainWindow = win; win.Show(); } } //<summary> //Entry point class to handle single instance of the application //</summary> public static class EntryPoint { [STAThread] public static void Main(string[] args) { Console.WriteLine("Main"); Console.ReadLine(); App app = new App(); app.Run(); } }
wpf 默認的Main函數在 app.g.cs文件中,如果我們想自己寫個Main,也可以的。
首先,寫好Main函數,如上面的代碼所示
然后,有兩個辦法,
1.Project-->Properties-->Application-- Start Object 默認是No Set,更改為 WPFMainTest.EntryPoint就可以了。
2.App.xaml 右擊選擇Properties,Build Action 從ApplicationDefinition改為Page。
OK。