Create a custom Main method for WPF application

When you create a new WPF Project the project template will create an App.xaml for you. Since WPF does a little magic behind the scenes, there is no way to access the Main method of your application. In this case, you simply delete the App.xaml and create a new App.cs class with the following code:

public class App : System.Windows.Application
    {
        public Application()
        {
            StartupUri = new Uri("Window1.xaml", UriKind.Relative);
        }

        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.LoaderOptimization(LoaderOptimization.MultiDomainHost)]
        public static void Main()
        {
            Application app = new Application();
            EnsureApplicationResources();
            app.Run();
        }

private static void EnsureApplicationResources()
{

        // merge in your application resources

        Application.Current.Resources.MergedDictionaries.Add(

            Application.LoadComponent(

                new Uri("CustomAppTest;component/Resources/App.xaml",

                UriKind.Relative)) as ResourceDictionary);

    }

}

    }

http://blog.matthidinger.com/2008/10/12/Increase+AddIn+Performance+With+MAF+And+WPF+Using+LoaderOptimization.aspx

http://www.drwpf.com/blog/Home/tabid/36/EntryID/10/Default.aspx

~ by Martin on January 8, 2009.

2 Responses to “Create a custom Main method for WPF application”

  1. It was nice to see your blog.Just Keep Writing!

    ______________________________
    Don’t pay for your electricity any longer…
    Instead, the power company will pay YOU!

  2. интересно было прочитать.

Leave a Reply