简体   繁体   中英

Is it necessary to inherit System.Windows.Application base class for WPF Application?

In VS2015, when we generated new WPF project, it contains App.xaml(.cs). I suppose that it will be an entry point of WPF application. Then App.xaml.cs becomes like:

...
using System.Windows;

namespace WpfApplication
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }
}

Now, I remove inheritance of System.Windows.Application , still it seems to be working...

- public partial class App : Application
+ public partial class App

So let me ask: App class does not need to inherit System.Windows.Application ? What is the difference?

thanks.

No it is not required. In your code you have the class defined as partial , this allows your code and autogenerated code by the compiler to be combined.

In your App.xaml you have

<Application x:Class="WpfApplication.App" ...

This tells the compiler you are creating a object of type Application with the name WpfApplication.App so the auto-generated code will include the inherited type class. Once your WpfApplication.App code is combined with the auto-generated WpfApplication.App the inheritance is already defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM