WPF 定义了自己的 Main()
方法。我应该如何用我自己的 Main
方法替换它(通常)打开 WPF MainWindow
(例如,通过命令行参数添加非 WPF 脚本模式) ?
最佳答案
一些示例描述了将 App.xaml 的构建操作从 ApplicationDefinition
更改为 Page
并编写自己的 Main()
来实例化 App
类并调用其 Run()
方法,但这可能会在 App.xaml 中解析应用程序范围资源时产生一些不良后果。
相反,我建议在自己的类中创建自己的 Main()
并在项目属性中将启动对象设置为该类:
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
View (M-V-VM) 在设计时无法解析 App.xaml 中保存的资源。
关于.net - 替换 WPF 入口点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6156550/