有多个入口点定义错误
我有以下代码:
namespace WpfApplication2 { /// /// Interaction logic for MainWindow.xaml /// 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程序’c:… \ WpfApplication2 \ WpfApplication2 \ obj \ Debug \ WpfApplication2.exe’定义了多个入口点:’WpfApplication2.Program.Main()’。 使用/ main编译以指定包含入口点的类型。
据我所知,我的代码上没有“程序”文件。 我不知道如何解决这个问题。 你能告诉我怎么修理吗? 谢谢。
您有两个主要选项可以在启动活动上实施
事件处理程序
App.xaml中
定义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 } }