2

I would like to override orignal main method in WPF.

I want add content at the beginnig of the origina main method. How to do it?

It seems that it has to be done in App.xaml.cs file, but still don't know how to achieve it.

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    To add own content at startup you can override [Application.OnStartup Method](http://msdn.microsoft.com/en-us/library/system.windows.application.onstartup%28v=vs.110%29.aspx). – LPL Jan 13 '14 at 17:11
  • 1
    what do you want this for? simply override `Application.OnStartup()` in the `App.xaml.cs`, while leaving the `Main()` method alone. A WPF application is not a console application. – Federico Berasategui Jan 13 '14 at 17:12
  • @LPL I want to add my content before: Application.Run() is invoked. If it would work could you show me an example. Tried many things and I am "little" lost. Where I should override it how it should look. – Yoda Jan 13 '14 at 17:13

3 Answers3

6

I don't believe you can, directly. The designer introduces its own Main method.

What you can do is create your own separate class with a Main method, which in turn calls App.Main when you want to:

using System;

namespace AppWithCustomMain
{
    class CustomMain
    {
        [STAThread]
        static void Main()
        {
            Console.WriteLine("CustomMain!");
            App.Main();
        }
    }
}

Then set the "startup object" build setting in your project properties to CustomMain, and it should call your Main method first, which in turn calls into App.Main.

This is assuming you really need to get in before anything else, however. Normally you'd just either subscribe to the Application.Startup event, or override Application.OnStartup within your Application subclass.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @JonSkeet Do you have any bad experiences with overloading the `Run`-method? Was my first thought. – Stefan Over Jan 13 '14 at 17:19
  • Is Herdo idea is ok? It worked well and it is solution for this question of mine: http://stackoverflow.com/questions/21095608/code-starts-only-when-application-run-is-invoked-wpf-application – Yoda Jan 13 '14 at 17:23
  • The solution Herdo gave is for this problem. Do you know why it happens? http://stackoverflow.com/questions/21095608/code-starts-only-when-application-run-is-invoked-wpf-application/21097554#21097554 – Yoda Jan 13 '14 at 17:49
  • @Herdo: Well, I personally think it's less clear - introducing a *new* method which happens to be called by the generated code doesn't seem ideal to me. This isn't really overloading - it's introducing a new method. In particular, if the generated code ever changes the type used to declare the `app` local variable in `Main` from `App` to `Application`, your code would break. You want a custom entry point, and this is what gives you that. – Jon Skeet Jan 13 '14 at 17:51
4

You can introduce a new Run()-method in your App class (yes, you're right - it has to be done inside the App.xaml.cs - , make your stuff and then call the base implementation:

public partial class App : Application
{
    public new void Run()
    {
        // Do your stuff here

        // Call the base method
        base.Run();
    }
}
Stefan Over
  • 5,851
  • 2
  • 35
  • 61
  • Please post this as an answer here: http://stackoverflow.com/questions/21095608/code-starts-only-when-application-run-is-invoked-wpf-application – Yoda Jan 13 '14 at 17:23
  • This *isn't* overloading - it's introducing a new method declaration with the same signature as an existing one, and it's brittle IMO. The current generated code uses `App app = ...; app.Run();` but if that's ever changed to `Application app = ...; app.Run();` (or something similar) then the code would break very subtly. – Jon Skeet Jan 13 '14 at 17:52
  • @JonSkeet Sorry, my bad. I corrected it to "introducing". You're right. But I'm really sure that Microsoft won't change the application entry point of existing applications at any point in time. Yoda and his team has to make sure that nobody will change the entry point, yep. – Stefan Over Jan 13 '14 at 18:44
  • @Herdo: Yes, I'm reasonably confident that it won't actually break - but I don't like having that sort of brittleness in an app for no particularly good reason. Given that Yoda actually wants a new entry point, why not just do that instead? – Jon Skeet Jan 13 '14 at 18:47
1

You shouldn't really override the main method of the application, if you want to call a specific method when your application starts up, you can override the OnStartup method of the App class (file App.xaml.cs).

Here's an example:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        // Your code here
    }
}
Gimly
  • 5,975
  • 3
  • 40
  • 75
  • 2
    I need to add content before `Application.Run()` is invoked. Will that work in that case? How to override it and call the orignal `OnStartup` or maybe it is undefinded. – Yoda Jan 13 '14 at 17:14
  • @Yoda please clarify `why` you need to add anything before `Application.Run()` – Federico Berasategui Jan 13 '14 at 17:15
  • 1
    @HighCore It is because of `Bonjour` in C# in winForms it will only work if you define `EvenManagers`(from Bonjour) before you call `Application.Run()` I need that in WPF application. – Yoda Jan 13 '14 at 17:16
  • @Yoda What about using a static constructor? – John Gibb Jan 13 '14 at 18:43
  • @JohnGibb I did not solve my problem..... exceptions TargetInvocationException – Yoda Jan 13 '14 at 19:38