How to modify Cortana Voice Activation commands (XML) in C#?
Asked Answered
A

1

6

I currently have Cortana implemented into my Silverlight app. The voice commands are stored in CortanaCommands.xml, here is the code:

<?xml version="1.0" encoding="utf-8"?>

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">
<CommandSet xml:lang="en-US">
<CommandPrefix>Dr. Bailey</CommandPrefix>
<Example> Open app to take dictation </Example>

<Command Name="Text">
  <Example> Is it going to rain? </Example>
  <ListenFor> [create] {dictation} </ListenFor>
  <Feedback> "" </Feedback>
  <Navigate Target="/Views/CortanaText.xaml" />
</Command>

<PhraseTopic Label="dictation" Scenario="Dictation">
  <Subject> Cal 123 </Subject>
</PhraseTopic>
</CommandSet>
</VoiceCommands>"

In this example, if the user deploys Cortana and says "Dr. Bailey, is it going to rain?", then the app navigates to the CortanaText.xaml. This is hard coded into the XML, and I want the user to be able to customize their Command Prefix and their Command.

By utilizing 2 textboxes, I have coded in C# a string that contains the entire XML, but inserting the choices from the the text boxes that contain the new Command Prefix and Command. This string is called cortanaXMLstring. What is the best possible way to overwrite the existing code in CortanaCommands.xml with the new string? I figured that would be easier than modifying 2 different areas of the existing XML. I also thought it might be possible to delete the CortanaCommands.xml (using c# code), and then creating that XML all over again by simply inserting the string, since the string contains all of the text needed for the XML? Or, are there any other suggestions on how to get these 2 fields in the XML modified? It's the Command Prefix and the Command Example. Thank you for any help that can be provided!

Abigael answered 27/12, 2014 at 19:21 Comment(0)
C
0

So we solved this after some amount of time.

First, we loaded the XML file into Isolated Storage and loaded the VCD file from isolated storage using

await VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri("ms-appdata:///local/CortanaCommands.xml", UriKind.RelativeOrAbsolute));

instead of

ms-appx:///CortanaCommands.xml.

the

"ms-appdata:///local/CortanaCommands.xml"

Is what loads from the Isolated Storage folder instead from the XAP file.

Then we completely rewrote the xml file in code replacing the

<CommandPrefix> and the <ListenFor>

with user input from the two text boxes. We loaded that into a stream and used

document.Root.RemoveAll();

to remove the old xml information from CortanaCommands.xml and rewrote the stream into xml using our new data and reloaded

VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri("ms-appdata:///local/CortanaCommands.xml", UriKind.RelativeOrAbsolute));

Doing all that allows for the user to add custom Command Prefix and phrase to launch the page we wanted to launch. This can be used to change anything in the xml file and completely rewrite the VCD to the users liking.

I should mention that for our purposes, we did not need the phrase topic as we only were using one navigation command. We deleted that phrase topic as it is optional anyway and changed the [create] {dictation} in the listenfor to "is it going to rain"

Camboose answered 12/1, 2015 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.