替换 WPF 入口点 [英] Replacing the WPF entry point

查看:32
本文介绍了替换 WPF 入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF 定义了自己的 Main() 方法.我应该如何用我自己的 Main 方法替换它(通常)打开 WPF MainWindow (例如,通过命令行参数添加非 WPF 脚本模式)?

WPF defines its own Main() method. How should I go about replacing it with my own Main method that (normally) opens the WPF MainWindow (e.g. to add a non-WPF scripting mode via command-line arguments)?

推荐答案

一些示例描述了将 App.xaml 的构建操作从 ApplicationDefinition 更改为 Page 并编写您自己的 Main() 实例化 App 类并调用其 Run() 方法,但这会在应用程序范围的资源解析中产生一些不需要的结果在 App.xaml 中.

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.

相反,我建议在自己的类中创建自己的 Main() 并在项目属性中将启动对象设置为该类:

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();
        }
    }
}

我这样做是为了利用一些必须在其他任何事情发生之前订阅的 AppDomain 事件(例如 AssemblyResolve).将 App.xaml 设置为 Page 的不良后果包括我的 UserControl 视图 (MV-VM) 在设计时无法解析 App.xaml 中保存的资源.

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.

这篇关于替换 WPF 入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆