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

Advertisements

~ by Martin on January 8, 2009.

4 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. интересно было прочитать.

  3. This is not required.
    You should change the build action of App.xaml to Page, add a Program.cs and a Main, then from the project properties select the Program class as the startup.

  4. Thanks for wheiging in and I couldn’t agree with you more Dennis. That’s specifically why I mentioned the idea of getting a few advisors who are potential CEOs, it’s a wonderful way to see how you work together. Click http://link.mx/hool08200

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: