简体   繁体   English

WPF 中没有 Main() 吗?

[英]No Main() in WPF?

I am a beginner when it comes to programming but I was sure that one of the universal rules was that a program starts with Main().我是编程的初学者,但我确信通用规则之一是程序以 Main() 开头。 I do not see one when I create a WPF project.我在创建 WPF 项目时没有看到。 Is Main() simply named something differently in WPF? Main() 只是在 WPF 中命名不同吗?

The Main() method is created automatically. Main() 方法是自动创建的。 If you want to provide your own you have to (tested in VS2013, VS2017 and VS2019):如果你想提供你自己的,你必须(在 VS2013、VS2017 和 VS2019 中测试过):

  • Right-click App.xaml in the solution explorer, select Properties在解决方案资源管理器中右键单击 App.xaml,选择属性
  • Change 'Build Action' to 'Page' (initial value is 'ApplicationDefinition')将“构建操作”更改为“页面”(初始值为“ApplicationDefinition”)

Then just add a Main() method to App.xaml.cs.然后只需将 Main() 方法添加到 App.xaml.cs。 It could be like this:它可能是这样的:

[STAThread]
public static void Main()
{
    var application = new App();
    application.InitializeComponent();
    application.Run();
}

It is generated during build, but you can provide your own (disambiguating it in project-properties as necessary).它是在构建期间生成的,但您可以提供自己的(根据需要在项目属性中消除歧义)。 Look in obj/debug for an app file;在 obj/debug 中查找应用程序文件; I have (courtesy of "C# 2010 Express") App.gics with:我有(由“C# 2010 Express”提供) App.gics

namespace WpfApplication1 {


    /// <summary>
    /// App
    /// </summary>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public partial class App : System.Windows.Application {

        /// <summary>
        /// InitializeComponent
        /// </summary>
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void InitializeComponent() {

            #line 4 "..\..\..\App.xaml"
            this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);

            #line default
            #line hidden
        }

        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public static void Main() {
            WpfApplication1.App app = new WpfApplication1.App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

Main() is automatically provided by the CLR and the WPF. Main()由 CLR 和 WPF 自动提供。

The C# compiler takes a command-line switch /m which specifies the type that contains the implementation of Main() . C# 编译器采用命令行开关/m来指定包含Main()实现的类型。 By convention, if no startup object is explicitly specified, the CLR will lookup any class that has a static Main() method and will call it.按照惯例,如果没有显式指定启动对象,CLR 将查找任何具有静态Main()方法的类并调用它。 (As @Marc Gravel pointed out in his comment) (正如@Marc Gravel 在评论中指出的那样)

In the case of WPF, the Main() is automatically generated when App.xaml is built and the /m switch is specified to make the C# compiler use that class as entry point.在 WPF 的情况下, Main()在构建App.xaml时自动生成,并指定 /m 开关以使 C# 编译器使用该类作为入口点。 If you look at the project properties however, you'll find there's a setting for you to choose the startup object.但是,如果您查看项目属性,您会发现有一个设置供您选择启动对象。 So if you want, you can provide your own class that implements Main() .因此,如果您愿意,可以提供自己的实现Main()

Note that this will put the responsibility on you to create the Application instance and call its Run() method to ensure that the WPF infrastructure is started properly.请注意,这将使您有责任创建Application实例并调用其Run()方法以确保 WPF 基础结构正确启动。

Main() is generated during compilation. Main()在编译期间生成。 You can find it in App.g.cs (in obj/{Debug,Release} folder).您可以在App.g.cs (在obj/{Debug,Release}文件夹中)中找到它。

main() is a standard entry point for an application, but all applications are structured that way. main()是应用程序的标准入口点,但所有应用程序都是以这种方式构建的。 In a XAML project, the App.XAML file specifies the entry point where it says StartupUri="MainWindow.xaml" .在 XAML 项目中,App.XAML 文件指定入口点,其中显示StartupUri="MainWindow.xaml"

As it is stated by others, the actual main function is generated based on the contents of the XAML files in the project.正如其他人所说,实际的主要功能是根据项目中 XAML 文件的内容生成的。

I copied files that wouldn't load in another project that was without a mainwindow into a new one and got this error.我将无法在没有主窗口的另一个项目中加载的文件复制到一个新项目中并出现此错误。

For me it took doing the opposite approach to Andreas Kahler to fix:对我来说,与 Andreas Kahler 采取相反的方法来修复:

After making a window file and setting the startup uri to this file i switched Page to ApplicationDefinition of App.xaml 'Build Action' property.创建一个窗口文件并将启动 uri 设置为此文件后,我将 Page 切换到 App.xaml 'Build Action' 属性的 ApplicationDefinition。

In case you removed default App.xaml and MinWindow.xaml, better to edit .csproj After adding App.xaml manually, your .csproj will be:如果您删除了默认的 App.xaml 和 MinWindow.xaml,最好编辑 .csproj 手动添加 App.xaml 后,您的 .csproj 将是:

<Page Include ="App.xaml">
       <DependentUpon>MSBuild:Compile</DependentUpon>
       <SubType>Code</SubType>
</Page>

Change this to:将此更改为:

<ApplicationDefinition Include="App.xaml">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
</ApplicationDefinition>

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

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