1

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 isn't any "Program" file in my code as far as I see. How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jason
  • 6,962
  • 36
  • 117
  • 198

2 Answers2

5

You have two primary options to implement on start activities:

Event handlers

File 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 file App.xaml.cs:

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

Override method

File 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
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • This might be the intended way to do this, but it's still possible to specify a custom Main method: http://stackoverflow.com/questions/6156550/replacing-the-wpf-entry-point – Roman Starkov Nov 27 '15 at 17:56
0

In order to solve this error, don't change the name of the .cs file when creating new code.

If you want a new name for your .cs file then you have to delete everything in the folder where your project is i.e., the fragments of files (bin, obj folders, etc.).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ALi
  • 1
  • 1