public static async Task RegisterMediaElementVoiceCommands(string customVoiceCommandFileName = null, bool registerMissingLocales = true)
{
try
{
// Define a temporary voice file into which the BuildIt voice command will be copied
// If a custom voice command file is specified, it will be merged with the BuildIt voice command before
// being written into the temporary voice file
var tempVoiceFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(BuildItVoiceCommandFileName, CreationCollisionOption.ReplaceExisting);
var assembly = typeof(CortanaListener).GetTypeInfo().Assembly;
using (var stream = assembly.GetManifestResourceStream(typeof(CortanaListener).Namespace + "." + BuildItVoiceCommandFileName))
using (var outStream = await tempVoiceFile.OpenStreamForWriteAsync())
{
var defaultXml = XDocument.Load(stream);
var customXml = defaultXml;
if (!string.IsNullOrWhiteSpace(customVoiceCommandFileName))
{
// A custom voice command file has been provided, so need to combine the commands
// Load the custom voice commands
customXml = XDocument.Load(customVoiceCommandFileName);
var buildItCommandSets = (from c in defaultXml.Descendants()
where VoiceCommandSchema.CommandSet == c.Name
select c).ToList();
var customCommandSets = (from c in customXml.Descendants()
where VoiceCommandSchema.CommandSet == c.Name
select c).ToList();
//var commandList = (from c in defaultXml.Descendants()
// where VoiceCommandSchema.Command == c.Name
// select c).ToList();
//var cutomCommandList = (from c in customXml.Descendants()
// where VoiceCommandSchema.Command == c.Name
// select c).ToList();
//var commandWithServiceList = (from c in commandList.Descendants()
// where VoiceCommandSchema.VoiceCommandService == c.Name
// select c).ToList();
//var customCommandWithService = (from c in cutomCommandList.Descendants()
// where VoiceCommandSchema.VoiceCommandService == c.Name
// select c).FirstOrDefault();
//if (!string.IsNullOrEmpty(appServiceName))
//{
// foreach (var command in commandWithServiceList)
// {
// command.Attribute("Target").Value = appServiceName;
// }
//}
//else
//{
// appServiceName = customCommandWithService.Attribute("Target").Value;
// foreach (var command in commandWithServiceList)
// {
// command.Attribute("Target").Value = appServiceName;
// }
//}
//var appName = (from c in customXml.Descendants()
// where VoiceCommandSchema.CommandSet == c.Name
// where c.Attribute(xmlns.GetName("lang")).Value == currentLocation
// let appNameCommandSetDesc = c.Descendants()
// from ac in appNameCommandSetDesc
// where VoiceCommandNameSpace.GetName("AppName") == ac.Name
// select ac.Value).FirstOrDefault();
//set commandSetList AppName to be appname if appname is not null
//var appName = Package.Current.DisplayName;
//if (!string.IsNullOrEmpty(appName))
//{
// appName = Package.Current.DisplayName;
// foreach (var command in buildItCommandSets)
// {
// var appNameNode = (from c in command.Descendants()
// where VoiceCommandNameSpace.GetName("AppName") == c.Name
// select c).FirstOrDefault();
// appNameNode.Value = appName;
// }
//}
var rootVoiceCommandsNode = customXml.FirstNode as XElement;
if (rootVoiceCommandsNode == null)
{
rootVoiceCommandsNode = new XElement(VoiceCommandSchema.VoiceCommands);
customXml.Add(rootVoiceCommandsNode);
}
foreach (var element in buildItCommandSets)
{
var matchFound = false;
foreach (var customElement in customCommandSets)
{
//add command nodes if the commandSet is existing
if (element.Attribute(VoiceCommandSchema.Lang).Value !=
customElement.Attribute(VoiceCommandSchema.Lang).Value) continue;
var commandNodes = (from c in element.Descendants()
where VoiceCommandSchema.Command == c.Name
select c).ToList();
var lastCommand = (from c in customElement.Descendants()
where VoiceCommandSchema.Command == c.Name
select c).LastOrDefault();
if (lastCommand != null)
{
lastCommand.AddAfterSelf(commandNodes);
}
else
{
customElement.Add(commandNodes);
}
matchFound = true;
break;
}
if (!matchFound)
{
rootVoiceCommandsNode.Add(element);
}
}
var allVoiceCommands = (from command in customXml.Descendants(VoiceCommandSchema.Command)
select command.SafeAttributeValue("Name")
).Distinct().ToList();
foreach (var commandName in allVoiceCommands)
{
await CopyActionIconsToTempFolder(commandName + ".png");
}
//add custom tile image
await CopyActionIconsToTempFolder("buildit_customTile.png");
// Get the name of the registered background app service for the app
// The BuildIt background taks has to be registered with this name
var inventoryService = new AppServiceConnection();
var appServiceName = inventoryService.AppServiceName;
var voiceCommandServiceNodes = (from c in customXml.Descendants()
where VoiceCommandSchema.VoiceCommandService == c.Name
select c).ToList();
var cutomCommandList = (from c in customXml.Descendants()
where VoiceCommandSchema.Command == c.Name
select c).ToList();
var customCommandWithService = (from c in cutomCommandList.Descendants()
where VoiceCommandSchema.VoiceCommandService == c.Name
select c).FirstOrDefault();
if (string.IsNullOrEmpty(appServiceName))
{
appServiceName = customCommandWithService.Attribute("Target").Value;
}
voiceCommandServiceNodes.DoForEach(node =>
{
var attribute = node.Attribute(VoiceCommandSchema.Target);
if (attribute == null)
{
attribute = new XAttribute(VoiceCommandSchema.Target, appServiceName);
node.Add(attribute);
}
else
{
attribute.Value = appServiceName;
}
});
}
// We need to make sure the app name
// element is updated with the name of the app (ie display name in the package)
var appName = Package.Current.DisplayName;
var appNameNodes = (from c in defaultXml.Descendants()
where VoiceCommandNameSpace.GetName("AppName") == c.Name
select c).ToList();
appNameNodes.DoForEach(appNameNode => appNameNode.Value = appName);
//save customXml
customXml.Save(outStream);
}
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(tempVoiceFile);
}
catch (Exception ex)
{
ex.LogException();
}
}