WPF 应用程序的入口点是什么? [英] What is the entry point of a WPF application?
问题描述
Main
方法是 C# 控制台应用程序的入口点.因此,例如,如果我必须启动一些线程或服务,我将在 Main
方法中进行.
The Main
method is the entry point of a C# console application. Thus, for example, if I have to start some threads or services, I will do it within the Main
method.
我在 WPF 项目中没有看到 Main
方法,那么 WPF 应用程序的入口点是什么?如果必须启动一些线程或服务,启动它们的代码应该写在哪里?
I do not see the Main
method inside a WPF project, so what is the entry point of a WPF application? If I have to start some threads or services, where should write the code for starting them?
更新: 这个答案总结了可用的解决方案,但有什么优点和缺点每个解决方案?
UPDATE: this answer summarizes the available solutions, but what are the pros and cons of each solution?
推荐答案
您的主要入口点是 App.Xaml 代码隐藏中 OnStartup 的覆盖:
Your main entry point is an override of OnStartup in the code-behind of App.Xaml :
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// here you take control
}
}
其他有趣的点可能是 Application.OnActivate() 以及 MainWindow 的 Loaded 和 Initialized 事件.
Other points of interest might be Application.OnActivate() and the Loaded and Initialized events of your MainWindow.
如果我必须启动一些线程或服务,应该在哪里编写启动它们的代码?
If I have to start some threads or services, where should write the code for starting them?
取决于这些线程/服务需要和想要什么.
Depends on what those threads/services need and want.
这篇关于WPF 应用程序的入口点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!