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.
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.
See more on this question at Stackoverflow