C# 4.0 在Windows Phone 8中定义短语列表的替代方法
我正在开发一个WindowsPhone8应用程序,它利用语音API Cortana来启动应用程序。我目前已经定义了一个短语列表,例如C# 4.0 在Windows Phone 8中定义短语列表的替代方法,c#-4.0,visual-studio-2013,speech-recognition,windows-phone-8.1,speech-synthesis,C# 4.0,Visual Studio 2013,Speech Recognition,Windows Phone 8.1,Speech Synthesis,我正在开发一个WindowsPhone8应用程序,它利用语音API Cortana来启动应用程序。我目前已经定义了一个短语列表,例如 <CommandSet xml:lang="en-US" Name="myLaunchCommand"> <CommandPrefix>Show Weather</CommandPrefix> <Example> Show Weather for Your City</Example&g
<CommandSet xml:lang="en-US" Name="myLaunchCommand">
<CommandPrefix>Show Weather</CommandPrefix>
<Example> Show Weather for Your City</Example>
<Command Name="StartApp">
<Example> For New York</Example>
<ListenFor> For {city} [City]</ListenFor>
<Feedback> Showing Weather for {city} </Feedback>
<Navigate Target="MainPage.xaml" />
</Command>
<PhraseList Label="city">
<Item>New Delhi</Item>
<Item>Delhi</Item>
<Item>Mumbai</Item>
<Item>Chennnai</Item>
<Item>Bangalore</Item>
<Item>Moradabad</Item>
<Item>New York</Item>
</PhraseList>
</CommandSet>
现在,我可以在短语列表中拥有无限数量的短语。在词汇表中定义世界上所有的城市确实是不可行的。有没有办法不定义预定义列表就从用户的语音中获取这些信息?如何处理我们没有提前获得短语的情况。您可以在运行时用代码调用存储库。从api检索国家并更新短语列表 例如,将其添加到viewmodel中
public async Task UpdatePhraseList()
{
try
{
VoiceCommandDefinition commandDefinitions;
string countryCode = CultureInfo.CurrentCulture.Name.ToLower();
if (countryCode.Length == 0)
{
countryCode = "en-us";
}
if (VoiceCommandDefinitionManager.InstalledCommandDefinitions.TryGetValue("VoiceCommandSet_" + countryCode, out commandDefinitions))
{
List<string> YourNewData = new List<string>();
await commandDefinitions.SetPhraseListAsync("yourkeyword", YourNewData);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Updating Phrase list for VCDs: " + ex.ToString());
}
它仍然不完全符合您的期望,但它至少为您提供了一种从资源构建它的方法
// Update phrase list.
ViewModel.ViewModelLocator locator = App.Current.Resources["ViewModelLocator"] as ViewModel.ViewModelLocator;
if (locator != null)
{
await locator.YourViewModel.UpdatePhraseList();
}