|
| 1 | +using ReactNative; |
| 2 | +using System; |
| 3 | +using Windows.ApplicationModel; |
| 4 | +using Windows.ApplicationModel.Activation; |
| 5 | +using Windows.UI.Xaml; |
| 6 | +using Windows.UI.Xaml.Controls; |
| 7 | +using Windows.UI.Xaml.Navigation; |
| 8 | + |
| 9 | +namespace Playground |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Provides application-specific behavior to supplement the default Application class. |
| 13 | + /// </summary> |
| 14 | + sealed partial class App : Application |
| 15 | + { |
| 16 | + private readonly ReactPage _reactPage; |
| 17 | + |
| 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 | + |
| 28 | + _reactPage = new MainPage(); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Invoked when the application is launched normally by the end user. Other entry points |
| 33 | + /// will be used such as when the application is launched to open a specific file. |
| 34 | + /// </summary> |
| 35 | + /// <param name="e">Details about the launch request and process.</param> |
| 36 | + protected override void OnLaunched(LaunchActivatedEventArgs e) |
| 37 | + { |
| 38 | + _reactPage.OnResume(Exit); |
| 39 | + |
| 40 | +#if DEBUG |
| 41 | + if (System.Diagnostics.Debugger.IsAttached) |
| 42 | + { |
| 43 | + this.DebugSettings.EnableFrameRateCounter = true; |
| 44 | + } |
| 45 | +#endif |
| 46 | + Frame rootFrame = Window.Current.Content as Frame; |
| 47 | + |
| 48 | + // Do not repeat app initialization when the Window already has content, |
| 49 | + // just ensure that the window is active |
| 50 | + if (rootFrame == null) |
| 51 | + { |
| 52 | + _reactPage.OnCreate(e.Arguments); |
| 53 | + |
| 54 | + // Create a Frame to act as the navigation context and navigate to the first page |
| 55 | + rootFrame = new Frame(); |
| 56 | + |
| 57 | + rootFrame.NavigationFailed += OnNavigationFailed; |
| 58 | + |
| 59 | + if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) |
| 60 | + { |
| 61 | + //TODO: Load state from previously suspended application |
| 62 | + } |
| 63 | + |
| 64 | + // Place the frame in the current Window |
| 65 | + Window.Current.Content = rootFrame; |
| 66 | + } |
| 67 | + |
| 68 | + if (e.PrelaunchActivated == false) |
| 69 | + { |
| 70 | + if (rootFrame.Content == null) |
| 71 | + { |
| 72 | + // When the navigation stack isn't restored navigate to the first page, |
| 73 | + // configuring the new page by passing required information as a navigation |
| 74 | + // parameter |
| 75 | + rootFrame.Content = _reactPage; |
| 76 | + } |
| 77 | + // Ensure the current window is active |
| 78 | + Window.Current.Activate(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Invoked when Navigation to a certain page fails |
| 84 | + /// </summary> |
| 85 | + /// <param name="sender">The Frame which failed navigation</param> |
| 86 | + /// <param name="e">Details about the navigation failure</param> |
| 87 | + void OnNavigationFailed(object sender, NavigationFailedEventArgs e) |
| 88 | + { |
| 89 | + throw new Exception("Failed to load Page " + e.SourcePageType.FullName); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Invoked when application execution is being suspended. Application state is saved |
| 94 | + /// without knowing whether the application will be terminated or resumed with the contents |
| 95 | + /// of memory still intact. |
| 96 | + /// </summary> |
| 97 | + /// <param name="sender">The source of the suspend request.</param> |
| 98 | + /// <param name="e">Details about the suspend request.</param> |
| 99 | + private void OnSuspending(object sender, SuspendingEventArgs e) |
| 100 | + { |
| 101 | + var deferral = e.SuspendingOperation.GetDeferral(); |
| 102 | + _reactPage.OnSuspend(); |
| 103 | + deferral.Complete(); |
| 104 | + } |
| 105 | + |
| 106 | + private void OnResuming(object sender, object e) |
| 107 | + { |
| 108 | + _reactPage.OnResume(Exit); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments