本文介绍了有多个入口点定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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);
        }
    }
}

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

When I run this code, I get the following error:

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

没有任何程序"据我所知,我的代码中的文件.我该如何解决这个问题?

There isn't any "Program" file in my code as far as I see. How can I fix this?

推荐答案

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

You have two primary options to implement on start activities:

文件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">

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

Define Startup="Application_Startup" and handle in file App.xaml.cs:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // On start stuff here
    }

覆盖方法

文件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
    }
}

这篇关于有多个入口点定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:53