C# base.OnStartup(e)做什么?

C# base.OnStartup(e)做什么?,c#,wpf,app.xaml,C#,Wpf,App.xaml,我看到很多人在App.xaml.cs中使用base.OnStartupe,如下所示: protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MaiWindow app = new MainWindow(); app.Show(); } 有必要吗?它的用途是什么?它允许运行任何基类逻辑;就像base的其他用法一样 这可能不是严格必要的;但是在重写虚拟方法时调用基类的实现被认为

我看到很多人在App.xaml.cs中使用base.OnStartupe,如下所示:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    MaiWindow app = new MainWindow();
    app.Show();
}

有必要吗?它的用途是什么?

它允许运行任何基类逻辑;就像base的其他用法一样


这可能不是严格必要的;但是在重写虚拟方法时调用基类的实现被认为是最佳实践,除非您主动想要抑制基类行为。

.NET Framework代码可以在

不包含太多功能:

/// <summary>
///     OnStartup is called to raise the Startup event. The developer will typically override this method
///     if they want to take action at startup time ( or they may choose to attach an event).
///     This method will be called once when the application begins, once that application's Run() method
///     has been called.
/// </summary>
/// <param name="e">The event args that will be passed to the Startup event</param>
protected virtual void OnStartup(StartupEventArgs e)
{
    // Verifies that the calling thread has access to this object.
    VerifyAccess();

    StartupEventHandler handler = (StartupEventHandler)Events[EVENT_STARTUP];
    if (handler != null)
    {
        handler(this, e);
    }
}
我们可以向启动事件添加处理程序,而不是覆盖OnStartup:

<Application x:Class="WpfApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="LaunchWpfApp"> 

private void LaunchWpfApp(object sender, StartupEventArgs e)
{
    MaiWindow app = new MainWindow();
    app.Show();
}

也看看这个答案:这真的不能回答这个问题。我知道Application.OnStartup的功能。我也明白我可以像代码中所示那样重写它。我的问题是为什么人们使用base.OnStartupe;Application.OnStartup的内部