Aug 5, 2010

Application Type in silverlgiht

When you create an silverlight app, by default a App.xaml file is created, and by default the class behind is the class or EntryPointType. This information is saved in the AppManifest.xml, so that when the xap file is downloaded, silverlight runtime will create an instance of that class. This class should derive from Application class. After the the application class is instantiated by the runtime, the most important job need to do in the constructor is to set the RootVisual property. You can do in code, or you can do it by loading a a xaml file. Visual studio use the second approach. And here is the pattern. The xaml file automatically create some code behind, which run the following code.


public partial class App1 : Application
    {
        private bool _contentLoaded;

        /// 
        /// InitializeComponent
        /// 
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public void InitializeComponent()
        {
            if (_contentLoaded)
            {
                return;
            }
            _contentLoaded = true;
            System.Windows.Application.LoadComponent(this, new System.Uri("/HelloWorldSilverlight;component/App1.xaml", System.UriKind.Relative));
        }


        public App1()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }
}