SetPhraseListAsync - 灾难性故障
本文关键字:故障 灾难性 SetPhraseListAsync | 更新日期: 2023-09-27 18:34:06
我正在尝试更新我的UWP(Windows 10)应用程序的短语列表,但更重要的是,我得到了一个"灾难性的失败",没有原因。
我在 App.xaml 中像这样注册 Cortana.cs
StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(@"VoiceCommandDefinition.xml");
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
Debug.WriteLine("Cortana loaded");
然后我加载我的数据(它是一个 Nest 恒温器客户端,所以我加载恒温器和结构)。之后,我尝试更新短语列表。
string thermostatsStrings = "";
string structureStrings = "";
VoiceCommandDefinition commandSetEnUs;
if (VoiceCommandDefinitionManager.InstalledCommandDefinitions.TryGetValue("Nest_en-us", out commandSetEnUs))
{
try
{
List<Nest.Thermostat> thermostats = GetAllThermostats();
if (thermostats != null)
{
thermostatsStrings = String.Join(", ", thermostats);
List<String> names = thermostats.Select(thermostat => thermostat.name).ToList();
await commandSetEnUs.SetPhraseListAsync("nest", names);
}
}
catch (Exception ex)
{
Crashes.Log("Helpers - UpdatePhraseListThermostat", true, ex.Message +
Environment.NewLine + "Thermostats: " + thermostatsStrings +
Environment.NewLine + "Structures: " + structureStrings);
}
try
{
List<Nest.Structure> structures = GetAllStructures();
if (structures != null)
{
structureStrings = String.Join(", ", structures);
List<String> names = structures.Select(structure => structure.name).ToList();
await commandSetEnUs.SetPhraseListAsync("place", names);
}
}
catch (Exception ex)
{
Crashes.Log("Helpers - UpdatePhraseListStructure", true, ex.Message +
Environment.NewLine + "Thermostats: " + thermostatsStrings +
Environment.NewLine + "Structures: " + structureStrings);
}
}
我的VCD文件看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.2">
<CommandSet xml:lang="en-US" Name="Nest_en-us">
<AppName> Nest </AppName>
<Example> Open Nest </Example>
<Command Name="RequestTemp">
<Example>Nest, what is the current temperature </Example>
<ListenFor> What is [the] {nest} temperature </ListenFor>
<ListenFor> What is [the] temperature in the {nest} </ListenFor>
<ListenFor> What is [the] [current] temperature </ListenFor>
<Feedback> Getting the temperature </Feedback>
<VoiceCommandService Target="NestCommandService"/>
</Command>
<Command Name="SetHome">
<Example> Nest, I'm coming home </Example>
<ListenFor> Set {place} to home </ListenFor>
<ListenFor> I am on my way to {place} </ListenFor>
<ListenFor> I am coming home </ListenFor>
<ListenFor> I am home </ListenFor>
<ListenFor> set [status] to home </ListenFor>
<ListenFor> set [state] to home </ListenFor>
<ListenFor> set [mode] to home </ListenFor>
<ListenFor> I am back </ListenFor>
<ListenFor> I am on my way [home] </ListenFor>
<Feedback> Setting mode to home </Feedback>
<VoiceCommandService Target="NestCommandService"/>
</Command>
<PhraseList Label="nest">
</PhraseList>
<PhraseList Label="place">
</PhraseList>
</CommandSet>
</VoiceCommands>
我在我的应用程序(MetroLog)中内置了错误日志记录,这是它记录的内容:
2|2016-02-03T15:39:14.0034437+00:00|TRACE|2|Crashes|Helpers - UpdatePhraseListThermostat
Catastrophic failure
Catastrophic failure
Thermostats: Living Room
Structures:
3|2016-02-03T16:03:32.2687198+00:00|TRACE|2|Crashes|Helpers - UpdatePhraseListStructure
Catastrophic failure
Catastrophic failure
Thermostats: Living Room
Structures: Home
代码中有一些事故。首先,如果你尝试从应用包安装 VCD,则代码看起来有点像这样:
Uri uriVoiceCommands = new Uri("ms-appx:///VoiceCommandDefinition1.xml", UriKind.Absolute);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uriVoiceCommands);
await VoiceCommandManager.InstallCommandSetsFromStorageFileAsync(file);
其次,在声明短语列表时,不建议不要在短语列表中插入任何元素,因此最初您必须至少放置 1 个元素。第三,灾难性错误可能是因为您正在运行大量代码来确定VCD的更新,我宁愿在单独的方法中确定要放入短语列表中的值,然后使用以下代码动态更新短语列表:
Windows.Media.SpeechRecognition.VoiceCommandSet commandSetEnUs;
if (Windows.Media.SpeechRecognition.VoiceCommandManager.InstalledCommandSets.TryGetValue("AdventureWorksCommandSet_en-us", out commandSetEnUs))
{
await commandSetEnUs.SetPhraseListAsync(
"destination", new string[] { Value1Det, value2Det }); //where Value2Det and Value1Det are the resulted outputs from thermostat
}