Wednesday, March 24, 2010

WPF - Overwrite the Main method

Sometimes we need larger control over our program. In WPF the framework hide the Main method from us (altough it can be found in the obj/debug(or release) directory in the App.g.cs file). Fortunately it's possible to overwrite this method:

Step 1: open the Visual Studio with the WPF project

Step 2: in Solution Explorer right click on the App.xaml select Properties and set the Build Action to Page (so we can define resources here, as normal)

Step3: add a new class file to the project:
public class MyApplication
{
    [STAThread]
    static public void Main(string[] args)
    {
        App app = new App(); //this is the default classname and not our application's
 name
        app.InitializeComponent();
        app.Run();
    }
}