/// <summary> /// OnActivated is the entry point for an application when it is launched via /// means other normal user interaction. This includes Voice Commands, URI activation, /// being used as a share target from another app, etc. Here, we're going to handle the /// Voice Command activation from Cortana. /// /// Note: Be aware that an older VCD could still be in place for your application if you /// modify it and update your app via the store. You should be aware that you could get /// activations that include commands in older versions of your VCD, and you should try /// to handle them gracefully. /// </summary> /// <param name="args">Details about the activation method, including the activation /// phrase (for voice commands) and the semantic interpretation, parameters, etc.</param> protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); Type navigationToPageType; ViewModel.TripVoiceCommand?navigationCommand = null; // If the app was launched via a Voice Command, this corresponds to the "show trip to <location>" command. // Protocol activation occurs when a tile is clicked within Cortana (via the background task) if (args.Kind == ActivationKind.VoiceCommand) { // The arguments can represent many different activation types. Cast it so we can get the // parameters we care about out. var commandArgs = args as VoiceCommandActivatedEventArgs; Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = commandArgs.Result; // Get the name of the voice command and the text spoken. See AdventureWorksCommands.xml for // the <Command> tags this can be filled with. string voiceCommandName = speechRecognitionResult.RulePath[0]; string textSpoken = speechRecognitionResult.Text; // The commandMode is either "voice" or "text", and it indictes how the voice command // was entered by the user. // Apps should respect "text" mode by providing feedback in silent form. string commandMode = this.SemanticInterpretation("commandMode", speechRecognitionResult); switch (voiceCommandName) { case "showTripToDestination": // Access the value of the {destination} phrase in the voice command string destination = this.SemanticInterpretation("destination", speechRecognitionResult); // Create a navigation command object to pass to the page. Any object can be passed in, // here we're using a simple struct. navigationCommand = new ViewModel.TripVoiceCommand( voiceCommandName, commandMode, textSpoken, destination); // Set the page to navigate to for this voice command. navigationToPageType = typeof(View.TripDetails); break; default: // If we can't determine what page to launch, go to the default entry point. navigationToPageType = typeof(View.TripListView); break; } } else if (args.Kind == ActivationKind.Protocol) { // Extract the launch context. In this case, we're just using the destination from the phrase set (passed // along in the background task inside Cortana), which makes no attempt to be unique. A unique id or // identifier is ideal for more complex scenarios. We let the destination page check if the // destination trip still exists, and navigate back to the trip list if it doesn't. var commandArgs = args as ProtocolActivatedEventArgs; Windows.Foundation.WwwFormUrlDecoder decoder = new Windows.Foundation.WwwFormUrlDecoder(commandArgs.Uri.Query); var destination = decoder.GetFirstValueByName("LaunchContext"); navigationCommand = new ViewModel.TripVoiceCommand( "protocolLaunch", "text", "destination", destination); navigationToPageType = typeof(View.TripDetails); } else { // If we were launched via any other mechanism, fall back to the main page view. // Otherwise, we'll hang at a splash screen. navigationToPageType = typeof(View.TripListView); } // Re"peat the same basic initialization as OnLaunched() above, taking into account whether // or not the app is already active. Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); App.NavigationService = new NavigationService(rootFrame); rootFrame.NavigationFailed += OnNavigationFailed; // Place the frame in the current Window Window.Current.Content = rootFrame; } // Since we're expecting to always show a details page, navigate even if // a content frame is in place (unlike OnLaunched). // Navigate to either the main trip list page, or if a valid voice command // was provided, to the details page for that trip. rootFrame.Navigate(navigationToPageType, navigationCommand); // Ensure the current window is active Window.Current.Activate(); }
/// <summary> /// OnActivated is the entry point for an application when it is launched via /// means other normal user interaction. This includes Voice Commands, URI activation, /// being used as a share target from another app, etc. Here, we're going to handle the /// Voice Command activation from Cortana. /// /// Note: Be aware that an older VCD could still be in place for your application if you /// modify it and update your app via the store. You should be aware that you could get /// activations that include commands in older versions of your VCD, and you should try /// to handle them gracefully. /// </summary> /// <param name="args">Details about the activation method, including the activation /// phrase (for voice commands) and the semantic interpretation, parameters, etc.</param> protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); Type navigationToPageType; ViewModel.TripVoiceCommand? navigationCommand = null; // If the app was launched via a Voice Command, this corresponds to the "show trip to <location>" command. // Protocol activation occurs when a tile is clicked within Cortana (via the background task) if (args.Kind == ActivationKind.VoiceCommand) { // The arguments can represent many different activation types. Cast it so we can get the // parameters we care about out. var commandArgs = args as VoiceCommandActivatedEventArgs; Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = commandArgs.Result; // Get the name of the voice command and the text spoken. See AdventureWorksCommands.xml for // the <Command> tags this can be filled with. string voiceCommandName = speechRecognitionResult.RulePath[0]; string textSpoken = speechRecognitionResult.Text; // The commandMode is either "voice" or "text", and it indictes how the voice command // was entered by the user. // Apps should respect "text" mode by providing feedback in silent form. string commandMode = this.SemanticInterpretation("commandMode", speechRecognitionResult); switch (voiceCommandName) { case "showTripToDestination": // Access the value of the {destination} phrase in the voice command string destination = this.SemanticInterpretation("destination", speechRecognitionResult); // Create a navigation command object to pass to the page. Any object can be passed in, // here we're using a simple struct. navigationCommand = new ViewModel.TripVoiceCommand( voiceCommandName, commandMode, textSpoken, destination); // Set the page to navigate to for this voice command. navigationToPageType = typeof(View.TripDetails); break; default: // If we can't determine what page to launch, go to the default entry point. navigationToPageType = typeof(View.TripListView); break; } } else if (args.Kind == ActivationKind.Protocol) { // Extract the launch context. In this case, we're just using the destination from the phrase set (passed // along in the background task inside Cortana), which makes no attempt to be unique. A unique id or // identifier is ideal for more complex scenarios. We let the destination page check if the // destination trip still exists, and navigate back to the trip list if it doesn't. var commandArgs = args as ProtocolActivatedEventArgs; Windows.Foundation.WwwFormUrlDecoder decoder = new Windows.Foundation.WwwFormUrlDecoder(commandArgs.Uri.Query); var destination = decoder.GetFirstValueByName("LaunchContext"); navigationCommand = new ViewModel.TripVoiceCommand( "protocolLaunch", "text", "destination", destination); navigationToPageType = typeof(View.TripDetails); } else { // If we were launched via any other mechanism, fall back to the main page view. // Otherwise, we'll hang at a splash screen. navigationToPageType = typeof(View.TripListView); } // Re"peat the same basic initialization as OnLaunched() above, taking into account whether // or not the app is already active. Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); App.NavigationService = new NavigationService(rootFrame); rootFrame.NavigationFailed += OnNavigationFailed; // Place the frame in the current Window Window.Current.Content = rootFrame; } // Since we're expecting to always show a details page, navigate even if // a content frame is in place (unlike OnLaunched). // Navigate to either the main trip list page, or if a valid voice command // was provided, to the details page for that trip. rootFrame.Navigate(navigationToPageType, navigationCommand); // Ensure the current window is active Window.Current.Activate(); }
/// <summary> /// OnActivated is the entry point for an application when it is launched via /// means other normal user interaction. This includes Voice Commands, URI activation, /// being used as a share target from another app, etc. Here, we're going to handle the /// Voice Command activation from Cortana. /// /// Note: Be aware that an older VCD could still be in place for your application if you /// modify it and update your app via the store. You should be aware that you could get /// activations that include commands in older versions of your VCD, and you should try /// to handle them gracefully. /// </summary> /// <param name="args">Details about the activation method, including the activation /// phrase (for voice commands) and the semantic interpretation, parameters, etc.</param> protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); // If the app was launched via some other mechanism than a Voice Command, exit. If // the app is able to act as a share target, or handle various file types, etc, // then developers should handle these cases here. if (args.Kind != ActivationKind.VoiceCommand) { return; } // The arguments can represent many different activation types. Cast it so we can get the // parameters we care about out. var commandArgs = args as VoiceCommandActivatedEventArgs; Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = commandArgs.Result; // Get the name of the voice command and the text spoken. See AdventureWorksCommands.xml for // the <Command> tags this can be filled with. string voiceCommandName = speechRecognitionResult.RulePath[0]; string textSpoken = speechRecognitionResult.Text; // The commandMode is either "voice" or "text", and it indictes how the voice command // was entered by the user. // Apps should respect "text" mode by providing feedback in silent form. string commandMode = this.SemanticInterpretation("commandMode", speechRecognitionResult); // Multiple different voice commands may be supported, switch between them (The voiceCommandName Type navigationToPageType; ViewModel.TripVoiceCommand?navigationCommand = null; switch (voiceCommandName) { case "showTripToDestination": // Access the value of the {destination} phrase in the voice command string destination = this.SemanticInterpretation("destination", speechRecognitionResult); // Create a navigation command object to pass to the page. Any object can be passed in, // here we're using a simple struct. navigationCommand = new ViewModel.TripVoiceCommand( voiceCommandName, commandMode, textSpoken, destination); // Set the page to navigate to for this voice command. navigationToPageType = typeof(View.TripDetails); break; default: // If we can't determine what page to launch, go to the default entry point. navigationToPageType = typeof(View.TripListView); break; } // Repeat the same basic initialization as OnLaunched() above, taking into account whether // or not the app is already active. Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); App.NavigationService = new NavigationService(rootFrame); rootFrame.NavigationFailed += OnNavigationFailed; // Place the frame in the current Window Window.Current.Content = rootFrame; } // Since we're expecting to always show a details page, navigate even if // a content frame is in place (unlike OnLaunched). // Navigate to either the main trip list page, or if a valid voice command // was provided, to the details page for that trip. rootFrame.Navigate(navigationToPageType, navigationCommand); // Ensure the current window is active Window.Current.Activate(); }
/// <summary> /// OnActivated is the entry point for an application when it is launched via /// means other normal user interaction. This includes Voice Commands, URI activation, /// being used as a share target from another app, etc. Here, we're going to handle the /// Voice Command activation from Cortana. /// /// Note: Be aware that an older VCD could still be in place for your application if you /// modify it and update your app via the store. You should be aware that you could get /// activations that include commands in older versions of your VCD, and you should try /// to handle them gracefully. /// </summary> /// <param name="args">Details about the activation method, including the activation /// phrase (for voice commands) and the semantic interpretation, parameters, etc.</param> protected override void OnActivated(IActivatedEventArgs args) { base.OnActivated(args); // If the app was launched via some other mechanism than a Voice Command, exit. If // the app is able to act as a share target, or handle various file types, etc, // then developers should handle these cases here. if (args.Kind != ActivationKind.VoiceCommand) { return; } // The arguments can represent many different activation types. Cast it so we can get the // parameters we care about out. var commandArgs = args as VoiceCommandActivatedEventArgs; Windows.Media.SpeechRecognition.SpeechRecognitionResult speechRecognitionResult = commandArgs.Result; // Get the name of the voice command and the text spoken. See AdventureWorksCommands.xml for // the <Command> tags this can be filled with. string voiceCommandName = speechRecognitionResult.RulePath[0]; string textSpoken = speechRecognitionResult.Text; // The commandMode is either "voice" or "text", and it indictes how the voice command // was entered by the user. // Apps should respect "text" mode by providing feedback in silent form. string commandMode = this.SemanticInterpretation("commandMode", speechRecognitionResult); // Multiple different voice commands may be supported, switch between them (The voiceCommandName Type navigationToPageType; ViewModel.TripVoiceCommand? navigationCommand = null; switch (voiceCommandName) { case "showTripToDestination": // Access the value of the {destination} phrase in the voice command string destination = this.SemanticInterpretation("destination", speechRecognitionResult); // Create a navigation command object to pass to the page. Any object can be passed in, // here we're using a simple struct. navigationCommand = new ViewModel.TripVoiceCommand( voiceCommandName, commandMode, textSpoken, destination); // Set the page to navigate to for this voice command. navigationToPageType = typeof(View.TripDetails); break; default: // If we can't determine what page to launch, go to the default entry point. navigationToPageType = typeof(View.TripListView); break; } // Repeat the same basic initialization as OnLaunched() above, taking into account whether // or not the app is already active. Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); App.NavigationService = new NavigationService(rootFrame); rootFrame.NavigationFailed += OnNavigationFailed; // Place the frame in the current Window Window.Current.Content = rootFrame; } // Since we're expecting to always show a details page, navigate even if // a content frame is in place (unlike OnLaunched). // Navigate to either the main trip list page, or if a valid voice command // was provided, to the details page for that trip. rootFrame.Navigate(navigationToPageType, navigationCommand); // Ensure the current window is active Window.Current.Activate(); }