WPF中如何重新定义Main函数

相信大家都知道,Main函数一直都作为程序的入口点,而在开发WPF项目的时候,有些初始化的操作则是想放在Main中去执行,那么当时想试试如果重新写一个Main函数后,程序会不会执行,不过结果很遗憾。

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace ProductManage
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public static void Main()
        {        
     
            ProductManage.App app = new ProductManage.App();
            app.InitializeComponent();
            app.Run();
        }
    }
}

编译完毕后就会出息如下错误:

这说明已经存在了一个Main函数了,重复定义便会出现编译错误;

  /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public static void Main() {
            ProductManage.App app = new ProductManage.App();
            app.InitializeComponent();
            app.Run();
        }

既然重复了,那么删掉原有的Main函数不就行了吗,删完之后,重新运行,果然程序正常运行;不过此时再把项目重新编译一下,还会出现上述Main函数重复定义的错误,治标不治本。

下面则是正确解决Main函数重复定义的方法

找到下面Main函数后删除

然后右键项目->属性得到如下所示:改为Application.App即可

最后 右键App.xml:生成操作改为Page,然后重新编译项目即可。

猜你喜欢

转载自www.cnblogs.com/QingYiShouJiuRen/p/11323049.html