WPF 從Main函數啟動


代碼:

/// <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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM