Replacing the WPF entry point

前端 未结 5 880
刺人心
刺人心 2020-11-29 20:06

WPF defines its own Main() method. How should I go about replacing it with my own Main method that (normally) opens the WPF MainWindow

5条回答
  •  悲哀的现实
    2020-11-29 20:50

    Some examples depict changing App.xaml's Build Action from ApplicationDefinition to Page and writing your own Main() that instantiates the App class and calls its Run() method, but this can produce some unwanted consequences in the resolution of application-wide resources in App.xaml.

    Instead, I suggest making your own Main() in its own class and setting the Startup Object to that class in the project properties:

    public class EntryPoint {
        [STAThread]
        public static void Main(string[] args) {
            if (args != null && args.Length > 0) {
                // ...
            } else {
                var app = new App();
                app.InitializeComponent();
                app.Run();
            }
        }
    }
    

    I do this to take advantage of some AppDomain events that must be subscribed to before anything else happens (such as AssemblyResolve). The unwanted consequences of setting App.xaml to Page that I experienced included my UserControl Views (M-V-VM) not resolving resources held in App.xaml during design-time.

提交回复
热议问题