简体   繁体   English

是否有必要为 WPF 应用程序继承 System.Windows.Application 基类?

[英]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).在 VS2015 中,当我们生成新的 WPF 项目时,它包含 App.xaml(.cs)。 I suppose that it will be an entry point of WPF application.我想这将是 WPF 应用程序的入口点。 Then App.xaml.cs becomes like:然后App.xaml.cs变成这样:

...
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...现在,我删除了System.Windows.Application继承,但它似乎仍然有效......

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

So let me ask: App class does not need to inherit System.Windows.Application ?那么请问: App类不需要继承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.在您的代码中,您将类定义为partial ,这允许您的代码和编译器自动生成的代码组合在一起。

In your App.xaml you have在你的 App.xaml 你有

<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.这告诉编译器您正在创建一个名为WpfApplication.App Application类型的对象,因此自动生成的代码将包含继承的类型类。 Once your WpfApplication.App code is combined with the auto-generated WpfApplication.App the inheritance is already defined.一旦您的WpfApplication.App代码与自动生成的WpfApplication.App结合,继承就已经定义了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF - 从System.Windows.Application继承 - WPF - inherit from System.Windows.Application 无法使用 System.Windows.Application - Can't use System.Windows.Application 在启动时在任务栏中显示wpf应用程序,而无需使用System.Windows.Application - Show wpf application on startup in taskbar without using System.Windows.Application System.Windows.Application类实例不会显示第二个窗口 - System.Windows.Application class instance won't show second window 错误:Application&#39;是&#39;System.Windows.Application&#39;和&#39;System.Windows.Forms.Application&#39;之间的模糊引用 - Error of: Application' is an ambiguous reference between 'System.Windows.Application' and 'System.Windows.Forms.Application' System.Windows.Forms.Application和System.Windows.Application之间的互操作性 - Interoperability between System.Windows.Forms.Application and System.Windows.Application 从另一个WPF应用程序加载WPF应用程序程序集,获取错误:无法在同一AppDomain中创建多个System.Windows.Application实例 - Load a WPF application Assembly from another WPF app, get error: Cannot create more than one System.Windows.Application instance in the same AppDomain How can i fix Application&#39; 是 &#39;System.Windows.Application&#39; 和 &#39;System.Windows.Forms.Application&#39; 之间的模棱两可的引用 - How can i fix Application' is an ambiguous reference between 'System.Windows.Application' and 'System.Windows.Forms.Application' 无法将类型为“ System.Windows.Application”的对象转换为类型为“ applicationName.App”的错误 - Unable to cast object of type 'System.Windows.Application' to type 'applicationName.App' error 在WPF应用程序中实现基本窗口类时出现System.ArgumentNullException - System.ArgumentNullException when Implementing base window class in WPF application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM