Has more than one entry point defined error

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have the following code :

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:

Error 1 Program 'c:...\WpfApplication2\WpfApplication2\obj\Debug\WpfApplication2.exe' has more than one entry point defined: 'WpfApplication2.Program.Main()'. Compile with /main to specify the type that contains the entry point.

There is no "Program"file on my code as far as I see. I don't know how to fix this. Can you tell me how to fix? Thanks.

回答1:

You have two primary option to implement on start activities

Event handlers

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

define Startup="Application_Startup" and handle in App.xaml.cs

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

Override method

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!