App.xaml.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using ReactNative;
  2. using ReactNative.Modules.Launch;
  3. using System;
  4. using Windows.ApplicationModel;
  5. using Windows.ApplicationModel.Activation;
  6. using Windows.UI.Core;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Navigation;
  10. namespace ViewShotExample
  11. {
  12. /// <summary>
  13. /// Provides application-specific behavior to supplement the default Application class.
  14. /// </summary>
  15. sealed partial class App : Application
  16. {
  17. private readonly ReactPage _reactPage;
  18. /// <summary>
  19. /// Initializes the singleton application object. This is the first line of authored code
  20. /// executed, and as such is the logical equivalent of main() or WinMain().
  21. /// </summary>
  22. public App()
  23. {
  24. this.InitializeComponent();
  25. this.Suspending += OnSuspending;
  26. this.Resuming += OnResuming;
  27. _reactPage = new MainPage();
  28. }
  29. /// <summary>
  30. /// Invoked when the application is launched normally by the end user. Other entry points
  31. /// will be used such as when the application is launched to open a specific file.
  32. /// </summary>
  33. /// <param name="e">Details about the launch request and process.</param>
  34. protected override void OnLaunched(LaunchActivatedEventArgs e)
  35. {
  36. base.OnLaunched(e);
  37. OnCreate(e.Arguments);
  38. }
  39. /// <summary>
  40. /// Invoked when the application is activated.
  41. /// </summary>
  42. /// <param name="args">The activated event arguments.</param>
  43. protected override void OnActivated(IActivatedEventArgs args)
  44. {
  45. base.OnActivated(args);
  46. switch (args.Kind)
  47. {
  48. case ActivationKind.Protocol:
  49. case ActivationKind.ProtocolForResults:
  50. var protocolArgs = (IProtocolActivatedEventArgs)args;
  51. LauncherModule.SetActivatedUrl(protocolArgs.Uri.AbsoluteUri);
  52. break;
  53. }
  54. if (args.PreviousExecutionState != ApplicationExecutionState.Running &&
  55. args.PreviousExecutionState != ApplicationExecutionState.Suspended)
  56. {
  57. OnCreate(null);
  58. }
  59. }
  60. /// <summary>
  61. /// Called whenever the app is opened to initia
  62. /// </summary>
  63. /// <param name="arguments"></param>
  64. private void OnCreate(string arguments)
  65. {
  66. _reactPage.OnResume(Exit);
  67. #if DEBUG
  68. if (System.Diagnostics.Debugger.IsAttached)
  69. {
  70. this.DebugSettings.EnableFrameRateCounter = true;
  71. }
  72. SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
  73. AppViewBackButtonVisibility.Visible;
  74. #endif
  75. Frame rootFrame = Window.Current.Content as Frame;
  76. // Do not repeat app initialization when the Window already has content,
  77. // just ensure that the window is active
  78. if (rootFrame == null)
  79. {
  80. _reactPage.OnCreate(arguments);
  81. // Create a Frame to act as the navigation context and navigate to the first page
  82. rootFrame = new Frame();
  83. rootFrame.NavigationFailed += OnNavigationFailed;
  84. // Place the frame in the current Window
  85. Window.Current.Content = rootFrame;
  86. }
  87. if (rootFrame.Content == null)
  88. {
  89. // When the navigation stack isn't restored navigate to the first page,
  90. // configuring the new page by passing required information as a navigation
  91. // parameter
  92. rootFrame.Content = _reactPage;
  93. }
  94. // Ensure the current window is active
  95. Window.Current.Activate();
  96. }
  97. /// <summary>
  98. /// Invoked when Navigation to a certain page fails
  99. /// </summary>
  100. /// <param name="sender">The Frame which failed navigation</param>
  101. /// <param name="e">Details about the navigation failure</param>
  102. private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
  103. {
  104. throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
  105. }
  106. /// <summary>
  107. /// Invoked when application execution is being suspended. Application state is saved
  108. /// without knowing whether the application will be terminated or resumed with the contents
  109. /// of memory still intact.
  110. /// </summary>
  111. /// <param name="sender">The source of the suspend request.</param>
  112. /// <param name="e">Details about the suspend request.</param>
  113. private void OnSuspending(object sender, SuspendingEventArgs e)
  114. {
  115. _reactPage.OnSuspend();
  116. }
  117. /// <summary>
  118. /// Invoked when application execution is being resumed.
  119. /// </summary>
  120. /// <param name="sender">The source of the resume request.</param>
  121. /// <param name="e">Details about the resume request.</param>
  122. private void OnResuming(object sender, object e)
  123. {
  124. _reactPage.OnResume(Exit);
  125. }
  126. }
  127. }