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屋!