【问题标题】:WPF Program 'XYZ' does not contain a static 'Main' method suitable for an entry pointWPF 程序“XYZ”不包含适用于入口点的静态“主”方法
【发布时间】:2015-10-08 21:15:04
【问题描述】:

当我在 VS 2013 中从模板创建新的 WPF 独立应用程序时,我无法在调试中针对此异常启动它。

错误 CS5001:程序“XYZ”不包含适合入口点的静态“Main”方法。该命令以代码 1 退出。

虽然我检查了 App.xaml 是否构建为 ApplicationDefinition = true,但项目是“Windows 应用程序”输出类型,我从命令行重新安装所有模板,我正在运行全新(重新安装)VS 2013 和 .net 4.5并且所有项目模板似乎都可以。 问题依然存在。

其他人也有这个吗?有类似的问题有答案,但没有人帮助我。所以这不是重复(版主)。

我不想为答案创建新线程,但在版主否决并在类似线程中删除此问题后,我不得不这样做。

【问题讨论】:

    标签: .net wpf visual-studio executable


    【解决方案1】:

    您的应用程序必须在项目代码目录中包含 App.xaml.cs 文件 比如这个

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace Lab2
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
        }
    }
    

    并且在路径为 obj\(Debug or Release)\的子目录中

    名称为 (App.i.g.cs) 的文件 (App.g.cs) 包含这样的内容

    #pragma checksum "..\..\App.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "E9A260C0E00B01D8D305634A08B2F0E3"
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.17929
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Automation;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Markup;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Effects;
    using System.Windows.Media.Imaging;
    using System.Windows.Media.Media3D;
    using System.Windows.Media.TextFormatting;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Shell;
    
    
    namespace Lab2 {
    
    
        /// <summary>
        /// App
        /// </summary>
        public partial class App : System.Windows.Application {
    
            /// <summary>
            /// InitializeComponent
            /// </summary>
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
            public void InitializeComponent() {
    
                #line 4 "..\..\App.xaml"
                this.StartupUri = new System.Uri("View/MainWindow.xaml", System.UriKind.Relative);
    
                #line default
                #line hidden
            }
    
            /// <summary>
            /// Application Entry Point.
            /// </summary>
            [System.STAThreadAttribute()]
            [System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
            public static void Main() {
                Lab2.App app = new Lab2.App();
                app.InitializeComponent();
                app.Run();
            }
        }
    }
    

    问题的解决方法可以在No Main() in WPF?找到

    【讨论】:

    • 我查看了 obj/Debug 文件夹,可以看到 App.xaml.g.i.cs 文件没有生成。然后我将“构建操作”从 ApplicationDefinition 切换到 Page 并显示文件。当然,其中没有 Main() 入口点。不确定为什么在“ApplicationDefinition”时在项目编译期间没有生成它。同样在 App.xaml 中将 StartupUri 设置为右主页。仍然是同样的例外。真正有效的是将 App.xaml 设置为 Page 并实现您在项目属性中设置的自己的 Main 作为起点。但是为什么它可以在没有它的情况下在我的第二台电脑上运行呢?还有其他想法吗?
    • 感谢提示“WPF 中没有 Main()?”但除了最后一条评论,你基本上创建了自己的 Main 方法,没有其他回复对我有用
    • 如果它在其他 PC 上编译,那么当前的 Visual Studio 副本有问题(可能与它的配置有关)。我不确定,但简单的重新安装可能会帮助你解决这个问题。
    • 我也试过这个。 VS 和模板重新安装,仍然出现问题:/ 还有什么可以尝试的提示吗?
    猜你喜欢
    • 2014-12-13
    • 2017-01-22
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    相关资源
    最近更新 更多