簡體   English   中英

有多個入口點定義錯誤

[英]Has more than one entry point defined error

我有以下代碼:

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 ​​程序 'c:...\\WpfApplication2\\WpfApplication2\\obj\\Debug\\WpfApplication2.exe' 定義了多個入口點:'WpfApplication2.Program.Main()'。 使用 /main 編譯以指定包含入口點的類型。

據我所知,我的代碼中沒有任何“程序”文件。 我怎樣才能解決這個問題?

您有兩個主要選項可以在開始活動時實施:

事件處理程序

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

為了解決這個錯誤,在創建新代碼時不要更改 .cs 文件的名稱。

如果您想為 .cs 文件命名新名稱,則必須刪除項目所在文件夾中的所有內容,即文件片段(bin、obj 文件夾等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM