有多个入口点定义错误

时间:2014-07-03 07:13:52

标签: 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);
        }
    }
}

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

  

错误1程序   &#39; C:\ WpfApplication2 \ WpfApplication2 \ OBJ \调试\ WpfApplication2.exe&#39;   定义了多个入口点:   &#39; WpfApplication2.Program.Main()&#39 ;.用/ main编译来指定   包含入口点的类型。

据我所知,我的代码上没有&#34; Program&#34;文件。我不知道如何解决这个问题。你能告诉我怎么修理吗?感谢。

2 个答案:

答案 0 :(得分:2)

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

活动处理程序

的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">

定义Startup="Application_Startup"并在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
    }
}

答案 1 :(得分:0)

为了解决此错误,在创建新文件时不要更改cs文件的名称 代码。

如果要为CS文件使用一个新名称,则必须删除项目所在文件夹中的所有内容,即文件的片段(bin,obj文件夹等)

相关问题