Today, we will talk about what happens when you compile a Windows Phone 7 application and what is the basic workflow of the application. There will be some concepts which may sound familiar to Silverlight developers.

When you compile a Windows Phone 7 Silverlight application, for each “.xaml” file 2 files are generated one with name “Filename.g.cs” and another one for “Filename.i.cs” for example App.xaml would result in “App.g.cs” and “App.i.cs”, where “g” is for generated and “i” is intellisense, These files would be in “obj\Debug” if your project is compiled in debug mode. There are bunch of other files in **“obj\Debug” **as well these files are file containing list of all of the files, compiled assembly of code behind files, etc.

All “.cs” and “resources” files are compiled and merged into final output which is nothing but few dlls and resources, Final output is inside “Bin\Debug”. These dlls and resource files are then zipped into a one single file which has extension ”.xap”. This “.xap” file would be there in “Bin\Debug” as well.This is the file that is deployed to the Windows Phone.

Let us talk about the workflow of the program. If you open **“App.xaml.cs” **(This file is there in visual studio solution). You will see following code in the constructor.


/// Constructor for the Application object. /// public App() { // Global handler for uncaught exceptions. UnhandledException += Application_UnhandledException; // Show graphics profiling information while debugging. if (System.Diagnostics.Debugger.IsAttached) { // Display the current frame rate counters. Application.Current.Host.Settings.EnableFrameRateCounter = true; // Show the areas of the app that are being redrawn in each frame. //Application.Current.Host.Settings.EnableRedrawRegions = true; // Enable non-production analysis visualization mode, // which shows areas of a page that are being GPU accelerated with a colored overlay. //Application.Current.Host.Settings.EnableCacheVisualization = true; } // Standard Silverlight initialization InitializeComponent(); // Phone-specific initialization InitializePhoneApplication(); }

First line of the constructor binds the error handler.Then there is a “if block” in which one can set values of some flags that control information shown to the developer while running the program in debugging mode. After that, constructor calls two methods first is “InitializeComponent()” and the other one is “InitializePhoneApplication()”.**“InitializeComponent()”** is a standard Silverlight initialization method that would be defined in the “.g.cs” class and it would be called here in the constructor of the App class. Where as **“InitializePhoneApplication()” **is specific function used for the initialization specific to phone.

private void InitializePhoneApplication() { if (phoneApplicationInitialized) return; // Create the frame but don't set it as RootVisual yet; this allows the splash // screen to remain active until the application is ready to render. RootFrame = new PhoneApplicationFrame(); RootFrame.Navigated += CompleteInitializePhoneApplication; // Handle navigation failures RootFrame.NavigationFailed += RootFrame_NavigationFailed; // Ensure we don't initialize again phoneApplicationInitialized = true; }

As you can see in the method **“InitializePhoneApplication()”**there is a flag that is maintained to ensure that this function would not be called twice. When application would be initialized for the first time it will create an Object of type “PhoneApplicationFrame” and assign that object to its property called “RootFrame” . Then, It will bind an event handler ”CompleteInitializePhoneApplication” to the event “Navigated” of the “RootFrame” , and at the end following code would be executed When this **“RootFrame” **would be Navigated to and that would be as soon as the application loads.

private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e) { // Set the root visual to allow the application to render if (RootVisual != RootFrame) RootVisual = RootFrame; // Remove this handler since it is no longer needed RootFrame.Navigated -= CompleteInitializePhoneApplication; }

Which will make sure that **“RootVisual” **Property of the **“App” **class is set to **“RootFrame”**and then it will remove the handler bindings.

In the next post we will talk about handling the application level events that are registered in with “PhoneApplicationService”