Advertisement
Guest User

App.xaml.cs

a guest
Nov 20th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.ApplicationModel;
  7. using Windows.ApplicationModel.Activation;
  8. using Windows.Foundation;
  9. using Windows.Foundation.Collections;
  10. using Windows.UI.Xaml;
  11. using Windows.UI.Xaml.Controls;
  12. using Windows.UI.Xaml.Controls.Primitives;
  13. using Windows.UI.Xaml.Data;
  14. using Windows.UI.Xaml.Input;
  15. using Windows.UI.Xaml.Media;
  16. using Windows.UI.Xaml.Navigation;
  17. using Windows.Media;
  18. using Windows.Media.SpeechSynthesis;
  19.  
  20. namespace Hello
  21. {
  22. /// <summary>
  23. /// Provides application-specific behavior to supplement the default Application class.
  24. /// </summary>
  25. sealed partial class App : Application
  26. {
  27. private MediaElement Media;
  28.  
  29. /// <summary>
  30. /// Initializes the singleton application object. This is the first line of authored code
  31. /// executed, and as such is the logical equivalent of main() or WinMain().
  32. /// </summary>
  33. public App()
  34. {
  35. this.InitializeComponent();
  36. this.Suspending += OnSuspending;
  37. this.Media = new MediaElement();
  38. this.SayHello();
  39. }
  40.  
  41. /// <summary>
  42. /// Invoked when the application is launched normally by the end user. Other entry points
  43. /// will be used such as when the application is launched to open a specific file.
  44. /// </summary>
  45. /// <param name="e">Details about the launch request and process.</param>
  46. protected override void OnLaunched(LaunchActivatedEventArgs e)
  47. {
  48. Frame rootFrame = Window.Current.Content as Frame;
  49.  
  50. // Do not repeat app initialization when the Window already has content,
  51. // just ensure that the window is active
  52. if (rootFrame == null)
  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.Navigate(typeof(MainPage), e.Arguments);
  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. //TODO: Save application state and stop any background activity
  103. deferral.Complete();
  104. }
  105.  
  106. private async void SayHello()
  107. {
  108. var synth = new SpeechSynthesizer();
  109. var stream = await synth.SynthesizeTextToStreamAsync("Hello world!");
  110. var media = this.Media;
  111. media.SetSource(stream, stream.ContentType);
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement