Advertisement
Liborek

App.xaml.cs with navigate

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