Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(probably dumb or too early to think)

I'm trying to add a Main function (entry point) to my empty WPF project, straight from the wizard.

public partial class App
{
    public static void Main(string[] args)
    {
      // do something...
    }
}


I get the error :

error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.


Am I missing something obvious ? can't I just add a Main function to do stuff ?

I know I can use OnStartup to do the same thing, but I have a demo/tutorial code that has the Main defined and I can't find why it's working.

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
}


What I have tried:

I've added this, if I understand correctly what the /main means.


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <StartupObject>WpfApp3.App</StartupObject>
  </PropertyGroup>

</Project>
Posted
Updated 27-Feb-23 10:44am

I think main is provided by the framework. The Application.XAML file defines which window is automatically loaded when it starts. See Create your first WPF app in Visual Studio 2019 - .NET Framework | Microsoft Learn[^]
 
Share this answer
 
This is normal if you build a WPF App by using the project template provided by Visual Studio.
Here is a way to accomplish what you want: WPF Applications with a Main Method[^]
 
Share this answer
 
As others have pointed out, Application.xaml and Application.xaml.vb are the application entry point.

By default, Application.xaml points to the MainWindow.xaml for startup. Here is what it looks like for a new project:
XML
<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp1"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        
    </Application.Resources>
</Application>

If you have ever tried to move the MainWindow.xaml to its own folder, if you do not modify the StartupUri, your application will not start.

If you want to run code before the MainWindow.xaml, then you need to modify the Application.xaml and remove the StartupUri. See below:
XML
<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApp1">
    <Application.Resources>
        
    </Application.Resources>
</Application>

Now it is your responsibility. We do this in the Application.xaml.vb file:
VB.NET
Class Application

    ' Application-level events, such as Startup, Exit, and
    '   DispatcherUnhandledException can be handled in this file.

    Public Sub New()

        ' catch all unhandled errors (if required)
        AddHandler AppDomain.CurrentDomain.UnhandledException, _
                   AddressOf OnUnhandledException

    End Sub

    Protected Overrides Sub OnStartup(e As StartupEventArgs)

        ' do work here

        Dim window = New MainWindow()
        window.Show()

    End Sub

    Protected Overrides Sub OnExit(e As ExitEventArgs)

        ' do clean up here - eg: save information

        MyBase.OnExit(e)

    End Sub



    Private Sub OnUnhandledException(sender As Object, e As UnhandledExceptionEventArgs)

        MessageBox.Show(
            DirectCast(e.ExceptionObject, Exception).Message,
            "Unhandled Error",
            MessageBoxButton.OK,
            MessageBoxImage.Stop)

    End Sub

End Class

Hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900