2017-10-06 3 views
0

Ich habe einen Befehl für Cortana, der einen Namen sagt, aber das funktioniert nur, wenn die App geöffnet ist. Wenn ich die Befehle mit der geschlossenen App ausspreche, öffnet sich die App, startet aber den Befehl nicht. Wie kann ich die App tatsächlich öffnen?App mit Cortana UWP öffnen

CortanaCommand.xml:

<CommandSet xml:lang="en-us" Name="ExampleAppCommandSet_en-us"> 
<CommandPrefix> Open name </CommandPrefix> 
<Example> Find a name </Example> 
<Command Name="FindName"> 
    <Example> Find name </Example> 
    <ListenFor> Find name </ListenFor> 
    <ListenFor> Find name </ListenFor> 
    <Feedback> Find name </Feedback> 
    <Navigate/> 
</Command> 

App.xaml.cs (public app):

public App() 
    { 
     Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
      Microsoft.ApplicationInsights.WindowsCollectors.Metadata | 
      Microsoft.ApplicationInsights.WindowsCollectors.Session); 
     this.InitializeComponent(); 
     this.Suspending += OnSuspending; 
    } 

App.xaml.cs (OnActivated):

protected override void OnActivated(IActivatedEventArgs e) 
    { 
     // Was the app activated by a voice command? 
     if (e.Kind != Windows.ApplicationModel.Activation.ActivationKind.VoiceCommand) 
     { 
      return; 
     } 

     var commandArgs = e as Windows.ApplicationModel.Activation.VoiceCommandActivatedEventArgs; 

     var speechRecognitionResult = commandArgs.Result; 
     string voiceCommandName = speechRecognitionResult.RulePath[0]; 
     string textSpoken = speechRecognitionResult.Text; 

     Frame rootFrame = Window.Current.Content as Frame; 
     MainPage page = rootFrame.Content as MainPage; 
     if (page == null) 
     { 
      return; 
     } 

     switch (voiceCommandName) 
     { 
      case "FindName": 
       page.FindNameInList(); 
       break; 

      default: 
       // There is no match for the voice command name. 
       break; 
     } 
    } 

MainPage.xaml.cs:

private async void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     var storageFile = 
      await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(
      new Uri("ms-appx:///CortanaCommand.xml")); 
     await Windows.ApplicationModel.VoiceCommands.VoiceCommandDefinitionManager 
      .InstallCommandDefinitionsFromStorageFileAsync(storageFile); 
    } 

    public async void FindNameInList() 
    { 
     MediaElement mediaElement = new MediaElement(); 

     // The object for controlling the speech synthesis engine (voice). 
     var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer(); 

     // Generate the audio stream from plain text. 
     SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("Your name, is, Jhon"); 

     // Send the stream to the media object. 
     mediaElement.SetSource(stream, stream.ContentType); 
     mediaElement.Play(); 
    } 

Vielen Dank im Voraus!

+0

Das ist also etwas, das in UWP nicht verfügbar ist. Der nächste, den Sie kommen können, ist, dass Sie die Cortana App öffnen und dann "FindName" sagen und es wird perfekt funktionieren –

Antwort