1 using System;
2 using System.Diagnostics;
3 using System.Resources;
4 using System.Windows;
5 using System.Windows.Markup;
6 using System.Windows.Navigation;
7 using Microsoft.Phone.Controls;
8 using Microsoft.Phone.Shell;
9 using PhoneXamlDirect3DApp1.Resources;
10 
11 namespace PhoneXamlDirect3DApp1
12 {
13     public partial class App : Application
14     {
15         /// <summary>
16         /// Provides easy access to the root frame of the Phone Application.
17         /// </summary>
18         /// <returns>The root frame of the Phone Application.</returns>
19         public static PhoneApplicationFrame RootFrame { get; private set; }
20 
21         /// <summary>
22         /// Constructor for the Application object.
23         /// </summary>
App()24         public App()
25         {
26             // Global handler for uncaught exceptions.
27             UnhandledException += Application_UnhandledException;
28 
29             // Standard XAML initialization
30             InitializeComponent();
31 
32             // Phone-specific initialization
33             InitializePhoneApplication();
34 
35             // Language display initialization
36             InitializeLanguage();
37 
38             // Show graphics profiling information while debugging.
39             if (Debugger.IsAttached)
40             {
41                 // Display the current frame rate counters.
42                 Application.Current.Host.Settings.EnableFrameRateCounter = true;
43 
44                 // Show the areas of the app that are being redrawn in each frame.
45                 //Application.Current.Host.Settings.EnableRedrawRegions = true;
46 
47                 // Enable non-production analysis visualization mode,
48                 // which shows areas of a page that are handed off to GPU with a colored overlay.
49                 //Application.Current.Host.Settings.EnableCacheVisualization = true;
50 
51                 // Prevent the screen from turning off while under the debugger by disabling
52                 // the application's idle detection.
53                 // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
54                 // and consume battery power when the user is not using the phone.
55                 PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
56             }
57 
58         }
59 
60         // Code to execute when the application is launching (eg, from Start)
61         // This code will not execute when the application is reactivated
Application_Launching(object sender, LaunchingEventArgs e)62         private void Application_Launching(object sender, LaunchingEventArgs e)
63         {
64         }
65 
66         // Code to execute when the application is activated (brought to foreground)
67         // This code will not execute when the application is first launched
Application_Activated(object sender, ActivatedEventArgs e)68         private void Application_Activated(object sender, ActivatedEventArgs e)
69         {
70         }
71 
72         // Code to execute when the application is deactivated (sent to background)
73         // This code will not execute when the application is closing
Application_Deactivated(object sender, DeactivatedEventArgs e)74         private void Application_Deactivated(object sender, DeactivatedEventArgs e)
75         {
76         }
77 
78         // Code to execute when the application is closing (eg, user hit Back)
79         // This code will not execute when the application is deactivated
Application_Closing(object sender, ClosingEventArgs e)80         private void Application_Closing(object sender, ClosingEventArgs e)
81         {
82         }
83 
84         // Code to execute if a navigation fails
RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)85         private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
86         {
87             if (Debugger.IsAttached)
88             {
89                 // A navigation has failed; break into the debugger
90                 Debugger.Break();
91             }
92         }
93 
94         // Code to execute on Unhandled Exceptions
Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)95         private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
96         {
97             if (Debugger.IsAttached)
98             {
99                 // An unhandled exception has occurred; break into the debugger
100                 Debugger.Break();
101             }
102         }
103 
104         #region Phone application initialization
105 
106         // Avoid double-initialization
107         private bool phoneApplicationInitialized = false;
108 
109         // Do not add any additional code to this method
InitializePhoneApplication()110         private void InitializePhoneApplication()
111         {
112             if (phoneApplicationInitialized)
113                 return;
114 
115             // Create the frame but don't set it as RootVisual yet; this allows the splash
116             // screen to remain active until the application is ready to render.
117             RootFrame = new PhoneApplicationFrame();
118             RootFrame.Navigated += CompleteInitializePhoneApplication;
119 
120             // Handle navigation failures
121             RootFrame.NavigationFailed += RootFrame_NavigationFailed;
122 
123             // Handle reset requests for clearing the backstack
124             RootFrame.Navigated += CheckForResetNavigation;
125 
126             // Ensure we don't initialize again
127             phoneApplicationInitialized = true;
128         }
129 
130         // Do not add any additional code to this method
CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)131         private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
132         {
133             // Set the root visual to allow the application to render
134             if (RootVisual != RootFrame)
135                 RootVisual = RootFrame;
136 
137             // Remove this handler since it is no longer needed
138             RootFrame.Navigated -= CompleteInitializePhoneApplication;
139         }
140 
CheckForResetNavigation(object sender, NavigationEventArgs e)141         private void CheckForResetNavigation(object sender, NavigationEventArgs e)
142         {
143             // If the app has received a 'reset' navigation, then we need to check
144             // on the next navigation to see if the page stack should be reset
145             if (e.NavigationMode == NavigationMode.Reset)
146                 RootFrame.Navigated += ClearBackStackAfterReset;
147         }
148 
ClearBackStackAfterReset(object sender, NavigationEventArgs e)149         private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
150         {
151             // Unregister the event so it doesn't get called again
152             RootFrame.Navigated -= ClearBackStackAfterReset;
153 
154             // Only clear the stack for 'new' (forward) and 'refresh' navigations
155             if (e.NavigationMode != NavigationMode.New && e.NavigationMode != NavigationMode.Refresh)
156                 return;
157 
158             // For UI consistency, clear the entire page stack
159             while (RootFrame.RemoveBackEntry() != null)
160             {
161                 ; // do nothing
162             }
163         }
164 
165         #endregion
166 
167         // Initialize the app's font and flow direction as defined in its localized resource strings.
168         //
169         // To ensure that the font of your application is aligned with its supported languages and that the
170         // FlowDirection for each of those languages follows its traditional direction, ResourceLanguage
171         // and ResourceFlowDirection should be initialized in each resx file to match these values with that
172         // file's culture. For example:
173         //
174         // AppResources.es-ES.resx
175         //    ResourceLanguage's value should be "es-ES"
176         //    ResourceFlowDirection's value should be "LeftToRight"
177         //
178         // AppResources.ar-SA.resx
179         //     ResourceLanguage's value should be "ar-SA"
180         //     ResourceFlowDirection's value should be "RightToLeft"
181         //
182         // For more info on localizing Windows Phone apps see http://go.microsoft.com/fwlink/?LinkId=262072.
183         //
InitializeLanguage()184         private void InitializeLanguage()
185         {
186             try
187             {
188                 // Set the font to match the display language defined by the
189                 // ResourceLanguage resource string for each supported language.
190                 //
191                 // Fall back to the font of the neutral language if the Display
192                 // language of the phone is not supported.
193                 //
194                 // If a compiler error is hit then ResourceLanguage is missing from
195                 // the resource file.
196                 RootFrame.Language = XmlLanguage.GetLanguage(AppResources.ResourceLanguage);
197 
198                 // Set the FlowDirection of all elements under the root frame based
199                 // on the ResourceFlowDirection resource string for each
200                 // supported language.
201                 //
202                 // If a compiler error is hit then ResourceFlowDirection is missing from
203                 // the resource file.
204                 FlowDirection flow = (FlowDirection)Enum.Parse(typeof(FlowDirection), AppResources.ResourceFlowDirection);
205                 RootFrame.FlowDirection = flow;
206             }
207             catch
208             {
209                 // If an exception is caught here it is most likely due to either
210                 // ResourceLangauge not being correctly set to a supported language
211                 // code or ResourceFlowDirection is set to a value other than LeftToRight
212                 // or RightToLeft.
213 
214                 if (Debugger.IsAttached)
215                 {
216                     Debugger.Break();
217                 }
218 
219                 throw;
220             }
221         }
222     }
223 }