具有多个已定义的入口点错误

jas*_*son 2 c# wpf

我有以下代码:

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            Width = 300; Height = 200; Title = "My Program Window";
            Content = "This application handles the Startup event.";
        }
    }

    class Program
    {
        static void App_Startup(object sender, StartupEventArgs args)
        {
            MessageBox.Show("The application is starting", "Starting Message");
        }

        [STAThread]
        static void Main()
        {
            MyWindow win = new MyWindow();

            Application app = new Application();
            app.Startup += App_Startup;

            app.Run(win);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,出现以下错误:

错误 1 ​​程序“c:...\WpfApplication2\WpfApplication2\obj\Debug\WpfApplication2.exe”定义了多个入口点:“WpfApplication2.Program.Main()”。使用 /main 进行编译以指定包含入口点的类型。

据我所知,我的代码中没有任何“程序”文件。我怎样才能解决这个问题?

pus*_*raj 5

您有两个主要选项可以在开始活动时实施:

事件处理程序

文件App.xaml

<Application x:Class="CSharpWPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             Startup="Application_Startup">
Run Code Online (Sandbox Code Playgroud)

在文件App.xaml.cs中定义Startup="Application_Startup"和处理:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // On start stuff here
    }
Run Code Online (Sandbox Code Playgroud)

覆盖方法

文件App.xaml.cs

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        // On start stuff here
        base.OnStartup(e);

        // Or here, where you find it more appropriate
    }
}
Run Code Online (Sandbox Code Playgroud)