Guest User

Untitled

a guest
Nov 5th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.49 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.Controls;
  8. using Microsoft.Phone.Shell;
  9. using System.IO.IsolatedStorage;
  10. using Facebook.Client;
  11.  
  12. namespace cocos2d
  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.  
  23.         enum SessionType
  24.         {
  25.             None,
  26.             Home,
  27.             DeepLink
  28.         }
  29.  
  30.         // Set to Home when the app is launched from Primary tile.
  31.         // Set to DeepLink when the app is launched from Deep Link.
  32.         private SessionType sessionType = SessionType.None;
  33.  
  34.         // Set to true when the page navigation is being reset
  35.         bool wasRelaunched = false;
  36.         Uri relaunchUri = null;
  37.  
  38.         // set to true when 5 min passed since the app was relaunched
  39.         bool mustClearPagestack = false;
  40.  
  41.         IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
  42.  
  43.  
  44.         /// <summary>
  45.         /// Constructor for the Application object.
  46.         /// </summary>
  47.         public App()
  48.         {
  49.             // Global handler for uncaught exceptions.
  50.             UnhandledException += Application_UnhandledException;
  51.             // Standard XAML initialization
  52.             InitializeComponent();
  53.             // Phone-specific initialization
  54.             InitializePhoneApplication();
  55.              // Show graphics profiling information while debugging.
  56.             if (Debugger.IsAttached)
  57.             {
  58.                 // Display the current frame rate counters.
  59.                 Application.Current.Host.Settings.EnableFrameRateCounter = false;
  60.                 // Show the areas of the app that are being redrawn in each frame.
  61.                 //Application.Current.Host.Settings.EnableRedrawRegions = true;
  62.  
  63.                 // Enable non-production analysis visualization mode,
  64.                 // which shows areas of a page that are handed off to GPU with a colored overlay.
  65.                 //Application.Current.Host.Settings.EnableCacheVisualization = true;
  66.  
  67.                 // Prevent the screen from turning off while under the debugger by disabling
  68.                 // the application's idle detection.
  69.                 // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
  70.                 // and consume battery power when the user is not using the phone.
  71.                 PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
  72.             }
  73.             Session.OnFacebookAuthenticationFinished += FacebookManager.Instance.OnFacebookAuthenticationFinished;
  74.             RootFrame.UriMapper = new FacebookUriMapper();
  75.  
  76.         }
  77.  
  78.  
  79.         // Code to execute when the application is launching (eg, from Start)
  80.         // This code will not execute when the application is reactivated
  81.         private void Application_Launching(object sender, LaunchingEventArgs e)
  82.         {
  83.  
  84.             // When a new instance of the app is launched, clear all deactivation settings
  85.             RemoveCurrentDeactivationSettings();
  86.         }
  87.  
  88.         // Code to execute when the application is activated (brought to foreground)
  89.         // This code will not execute when the application is first launched
  90.         private void Application_Activated(object sender, ActivatedEventArgs e)
  91.         {
  92.             // If some interval has passed since the app was deactivated (30 seconds in this example),
  93.             // then remember to clear the back stack of pages
  94.             mustClearPagestack = CheckDeactivationTimeStamp();
  95.  
  96.  
  97.             // If IsApplicationInstancePreserved is not true, then set the session type to the value
  98.             // saved in isolated storage. This will make sure the session type is correct for an
  99.             // app that is being resumed after being tombstoned.
  100.             if (!e.IsApplicationInstancePreserved)
  101.             {
  102.                 RestoreSessionType();
  103.             }
  104.  
  105.         }
  106.  
  107.         // Code to execute when the application is deactivated (sent to background)
  108.         // This code will not execute when the application is closing
  109.         private void Application_Deactivated(object sender, DeactivatedEventArgs e)
  110.         {
  111.             // When the applicaiton is deactivated, save the current deactivation settings to isolated storage
  112.             SaveCurrentDeactivationSettings();
  113.         }
  114.  
  115.         // Code to execute when the application is closing (eg, user hit Back)
  116.         // This code will not execute when the application is deactivated
  117.         private void Application_Closing(object sender, ClosingEventArgs e)
  118.         {
  119.             // When the application closes, delete any deactivation settings from isolated storage
  120.             RemoveCurrentDeactivationSettings();
  121.         }
  122.  
  123.         // Code to execute if a navigation fails
  124.         private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
  125.         {
  126.             if (Debugger.IsAttached)
  127.             {
  128.                 // A navigation has failed; break into the debugger
  129.                 Debugger.Break();
  130.             }
  131.         }
  132.  
  133.         // Code to execute on Unhandled Exceptions
  134.         private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
  135.         {
  136.             if (Debugger.IsAttached)
  137.             {
  138.                 // An unhandled exception has occurred; break into the debugger
  139.                 Debugger.Break();
  140.             }
  141.         }
  142.  
  143.         #region Phone application initialization
  144.  
  145.         // Avoid double-initialization
  146.         private bool phoneApplicationInitialized = false;
  147.  
  148.         // Do not add any additional code to this method
  149.         private void InitializePhoneApplication()
  150.         {
  151.             if (phoneApplicationInitialized)
  152.                 return;
  153.  
  154.             // Create the frame but don't set it as RootVisual yet; this allows the splash
  155.             // screen to remain active until the application is ready to render.
  156.             RootFrame = new PhoneApplicationFrame();
  157.             RootFrame.Navigated += CompleteInitializePhoneApplication;
  158.  
  159.             // Handle navigation failures
  160.             RootFrame.NavigationFailed += RootFrame_NavigationFailed;
  161.  
  162.             // Handle reset requests for clearing the backstack
  163.             RootFrame.Navigated += CheckForResetNavigation;
  164.  
  165.             // Monitor deep link launching
  166.             RootFrame.Navigating += RootFrame_Navigating;
  167.  
  168.             // Ensure we don't initialize again
  169.             phoneApplicationInitialized = true;
  170.         }
  171.  
  172.         // Event handler for the Navigating event of the root frame. Use this handler to modify
  173.         // the default navigation behavior.
  174.         void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
  175.         {
  176.  
  177.             // If the session type is None or New, check the navigation Uri to determine if the
  178.             // navigation is a deep link or if it points to the app's main page.
  179.             if (sessionType == SessionType.None && e.NavigationMode == NavigationMode.New)
  180.             {
  181.                 // This block will run if the current navigation is part of the app's intial launch
  182.  
  183.  
  184.                 // Keep track of Session Type
  185.                 if (e.Uri.ToString().Contains("DeepLink=true"))
  186.                 {
  187.                     sessionType = SessionType.DeepLink;
  188.                 }
  189.                 else if (e.Uri.ToString().Contains("/MainPage.xaml"))
  190.                 {
  191.                     sessionType = SessionType.Home;
  192.                 }
  193.             }
  194.  
  195.  
  196.             if (e.NavigationMode == NavigationMode.Reset)
  197.             {
  198.                 // This block will execute if the current navigation is a relaunch.
  199.                 // If so, another navigation will be coming, so this records that a relaunch just happened
  200.                 // so that the next navigation can use this info.
  201.                 wasRelaunched = true;
  202.                 relaunchUri = e.Uri;
  203.             }
  204.             else if (e.NavigationMode == NavigationMode.New && wasRelaunched)
  205.             {
  206.                 // This block will run if the previous navigation was a relaunch
  207.                 wasRelaunched = false;
  208.  
  209.                 if (e.Uri.ToString().Contains("DeepLink=true"))
  210.                 {
  211.                     // This block will run if the launch Uri contains "DeepLink=true" which
  212.                     // was specified when the secondary tile was created in MainPage.xaml.cs
  213.  
  214.                     sessionType = SessionType.DeepLink;
  215.                     // The app was relaunched via a Deep Link.
  216.                     // The page stack will be cleared.
  217.                 }
  218.                 else if (relaunchUri.ToString().Contains("/MainPage.xaml"))
  219.                 {
  220.                     // This block will run if the navigation Uri is the main page
  221.                     if (sessionType == SessionType.DeepLink)
  222.                     {
  223.                         // When the app was previously launched via Deep Link and relaunched via Main Tile, we need to clear the page stack.
  224.                         sessionType = SessionType.Home;
  225.                     }
  226.                     else
  227.                     {
  228.                         if (!mustClearPagestack)
  229.                         {
  230.                             //The app was previously launched via Main Tile and relaunched via Main Tile. Cancel the navigation to resume.
  231.                             e.Cancel = true;
  232.                             RootFrame.Navigated -= ClearBackStackAfterReset;
  233.                         }
  234.                     }
  235.                 }
  236.  
  237.                 //e.Cancel = true;
  238.                 //RootFrame.Navigated -= ClearBackStackAfterReset;
  239.                 mustClearPagestack = false;
  240.             }
  241.         }
  242.         // Do not add any additional code to this method
  243.         private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
  244.         {
  245.             // Set the root visual to allow the application to render
  246.             if (RootVisual != RootFrame)
  247.                 RootVisual = RootFrame;
  248.  
  249.             // Remove this handler since it is no longer needed
  250.             RootFrame.Navigated -= CompleteInitializePhoneApplication;
  251.         }
  252.  
  253.         private void CheckForResetNavigation(object sender, NavigationEventArgs e)
  254.         {
  255.             // If the app has received a 'reset' navigation, then we need to check
  256.             // on the next navigation to see if the page stack should be reset
  257.             if (e.NavigationMode == NavigationMode.Reset)
  258.                 RootFrame.Navigated += ClearBackStackAfterReset;
  259.         }
  260.  
  261.         private void ClearBackStackAfterReset(object sender, NavigationEventArgs e)
  262.         {
  263.             // Unregister the event so it doesn't get called again
  264.             RootFrame.Navigated -= ClearBackStackAfterReset;
  265.  
  266.             // Only clear the stack for 'new' (forward) and 'refresh' navigations
  267.             if (e.NavigationMode != NavigationMode.New)
  268.                 return;
  269.  
  270.             // For UI consistency, clear the entire page stack
  271.             while (RootFrame.RemoveBackEntry() != null)
  272.             {
  273.                 ; // do nothing
  274.             }
  275.         }
  276.  
  277.         #endregion
  278.  
  279.         // Helper method for adding or updating a key/value pair in isolated storage
  280.         public bool AddOrUpdateValue(string Key, Object value)
  281.         {
  282.             bool valueChanged = false;
  283.  
  284.             // If the key exists
  285.             if (settings.Contains(Key))
  286.             {
  287.                 // If the value has changed
  288.                 if (settings[Key] != value)
  289.                 {
  290.                     // Store the new value
  291.                     settings[Key] = value;
  292.                     valueChanged = true;
  293.                 }
  294.             }
  295.             // Otherwise create the key.
  296.             else
  297.             {
  298.                 settings.Add(Key, value);
  299.                 valueChanged = true;
  300.             }
  301.             return valueChanged;
  302.         }
  303.  
  304.         // Helper method for removing a key/value pair from isolated storage
  305.         public void RemoveValue(string Key)
  306.         {
  307.             // If the key exists
  308.             if (settings.Contains(Key))
  309.             {
  310.                 settings.Remove(Key);
  311.             }
  312.         }
  313.  
  314.         // Called when the app is deactivating. Saves the time of the deactivation and the
  315.         // session type of the app instance to isolated storage.
  316.         public void SaveCurrentDeactivationSettings()
  317.         {
  318.             if (AddOrUpdateValue("DeactivateTime", DateTimeOffset.Now))
  319.             {
  320.                 settings.Save();
  321.             }
  322.  
  323.             if (AddOrUpdateValue("SessionType", sessionType))
  324.             {
  325.                 settings.Save();
  326.             }
  327.  
  328.         }
  329.  
  330.         // Called when the app is launched or closed. Removes all deactivation settings from
  331.         // isolated storage
  332.         public void RemoveCurrentDeactivationSettings()
  333.         {
  334.             RemoveValue("DeactivateTime");
  335.             RemoveValue("SessionType");
  336.             settings.Save();
  337.         }
  338.  
  339.         // Helper method to determine if the interval since the app was deactivated is
  340.         // greater than 30 seconds
  341.         bool CheckDeactivationTimeStamp()
  342.         {
  343.             DateTimeOffset lastDeactivated;
  344.  
  345.             if (settings.Contains("DeactivateTime"))
  346.             {
  347.                 lastDeactivated = (DateTimeOffset)settings["DeactivateTime"];
  348.             }
  349.  
  350.             var currentDuration = DateTimeOffset.Now.Subtract(lastDeactivated);
  351.  
  352.             return TimeSpan.FromSeconds(currentDuration.TotalSeconds) > TimeSpan.FromSeconds(30);
  353.         }
  354.  
  355.         // Helper method to restore the session type from isolated storage.
  356.         void RestoreSessionType()
  357.         {
  358.             if (settings.Contains("SessionType"))
  359.             {
  360.                 sessionType = (SessionType)settings["SessionType"];
  361.             }
  362.         }
  363.         private void OnFacebookAuthenticationFinished(AccessTokenData session)
  364.         {
  365.             // here the authentication succeeded callback will be received.
  366.             // put your login logic here
  367.             System.Diagnostics.Debug.WriteLine("OnFacebookAuthenticationFinished");
  368.         }
  369.  
  370.     }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment