Xml code

--------------------------------

<Page

x:Class="MyApp.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="using:MyApp"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d"

Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>

<TextBlock Text="请说出你最喜欢的体育运动:" FontSize="18"/>

<Button Content="开始识别" Width="200" Click="OnClick"/>

<ListBox Name="lb">

<x:String>足球</x:String>

<x:String>排球</x:String>

<x:String>跑步</x:String>

<x:String>羽毛球</x:String>

<x:String>篮球</x:String>

</ListBox>

</StackPanel>

</Page>

C# code

------------------------

public sealed partial class MainPage : Page

{

SpeechRecognizer _recognizer = null;

public MainPage()

{

this.InitializeComponent();

this.NavigationCacheMode = NavigationCacheMode.Required;

this.Loaded += Page_Loaded;

this.Unloaded += Page_Unloaded;

}

private void Page_Unloaded(object sender, RoutedEventArgs e)

{

// 释放资源

_recognizer.Dispose();

}

private async void Page_Loaded(object sender, RoutedEventArgs e)

{

_recognizer = new SpeechRecognizer();

// 创建自定义短语约束

string[] array = { "足球", "排球", "跑步", "羽毛球", "篮球" };

SpeechRecognitionListConstraint listConstraint = new SpeechRecognitionListConstraint(array);

// 添加约束实例到集合中

_recognizer.Constraints.Add(listConstraint);

// 编译约束

await _recognizer.CompileConstraintsAsync();

}

private async void OnClick(object sender, RoutedEventArgs e)

{

Button btn = sender as Button;

btn.IsEnabled = false;

try

{

SpeechRecognitionResult res = await _recognizer.RecognizeAsync();

if (res.Status == SpeechRecognitionResultStatus.Success)

{

// 处理识别结果

this.lb.SelectedItem = res.Text;

}

}

catch { /* 忽略异常 */ }

btn.IsEnabled = true;

}

}

xml 文件

语音识别

public sealed partial class App : Application

{

/// <summary>

/// Initializes the singleton application object.  This is the first line of authored code

/// executed, and as such is the logical equivalent of main() or WinMain().

/// </summary>

public App()

{

this.InitializeComponent();

this.Suspending += this.OnSuspending;

}

/// <summary>

/// Invoked when the application is launched normally by the end user.  Other entry points

/// will be used when the application is launched to open a specific file, to display

/// search results, and so forth.

/// </summary>

/// <param name="e">Details about the launch request and process.</param>

protected async override void OnLaunched(LaunchActivatedEventArgs e)

{

StorageFile vcdFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///vcd.xml"));

// 安装VCD文件

await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdFile);

Frame rootFrame = Window.Current.Content as Frame;

// Do not repeat app initialization when the Window already has content,

// just ensure that the window is active

if (rootFrame == null)

{

// Create a Frame to act as the navigation context and navigate to the first page

rootFrame = new Frame();

// TODO: change this value to a cache size that is appropriate for your application

rootFrame.CacheSize = 1;

// Set the default language

rootFrame.Language = "zh-CN";

if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)

{

// TODO: Load state from previously suspended application

}

// Place the frame in the current Window

Window.Current.Content = rootFrame;

}

if (rootFrame.Content == null)

{

// When the navigation stack isn't restored navigate to the first page,

// configuring the new page by passing required information as a navigation

// parameter

if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))

{

throw new Exception("Failed to create initial page");

}

}

// Ensure the current window is active

Window.Current.Activate();

}

/// <summary>

/// Invoked when application execution is being suspended.  Application state is saved

/// without knowing whether the application will be terminated or resumed with the contents

/// of memory still intact.

/// </summary>

/// <param name="sender">The source of the suspend request.</param>

/// <param name="e">Details about the suspend request.</param>

private void OnSuspending(object sender, SuspendingEventArgs e)

{

var deferral = e.SuspendingOperation.GetDeferral();

// TODO: Save application state and stop any background activity

deferral.Complete();

}

protected override void OnActivated(IActivatedEventArgs args)

{

if (args.Kind == ActivationKind.VoiceCommand)

{

VoiceCommandActivatedEventArgs varg = (VoiceCommandActivatedEventArgs)args;

// 处理识别结果

SpeechRecognitionResult res = varg.Result;

// 获取已识别的指令名字

string cmdName = res.RulePath[0];

if (cmdName == "open")

{

// 获取PhraseList中被识别出来的项

var interpretation = res.SemanticInterpretation;

if (interpretation != null)

{

// 通过PhraseList的Label属性可以查询出被识别的Item

string item = interpretation.Properties["pages"].FirstOrDefault();

if (!string.IsNullOrEmpty(item))

{

// 导航到对应页面

Frame root = Window.Current.Content as Frame;

if (root == null)

{

root = new Frame();

Window.Current.Content = root;

}

switch (item)

{

case "我的音乐":

root.Navigate(typeof(MyMusicPage));

break;

case "我的视频":

root.Navigate(typeof(MyVedioPage));

break;

case "我的照片":

root.Navigate(typeof(MyPhotoPage));

break;

case "主页":

root.Navigate(typeof(MainPage));

break;

default:

root.Navigate(typeof(MainPage));

break;

}

}

}

}

}

Window.Current.Activate();

}

}

识别的xml 文件

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

<VoiceCommands xmlns="http://schemas.microsoft.com/voicecommands/1.1">

<CommandSet xml:lang="zh-cn">

<CommandPrefix>测试应用</CommandPrefix>

<Example>“打开 主页”或“打开 我的音乐”或“我的音乐”或“打开我的视频”或“我的视频”……</Example>

<Command Name="open">

<Example>“打开 我的音乐”或“我的音乐”</Example>

<ListenFor>[打开]{pages}</ListenFor>

<Feedback>好的,正在努力打开中……</Feedback>

<Navigate/>

</Command>

<PhraseList Label="pages">

<Item>主页</Item>

<Item>我的音乐</Item>

<Item>我的视频</Item>

<Item>我的照片</Item>

</PhraseList>

</CommandSet>

</VoiceCommands>

最新文章

  1. 北京全景视频外包公司:长年承接VR全景视频外包
  2. [LeetCode] 034. Search for a Range (Medium) (C++/Java)
  3. EFCore数据库迁移命令整理
  4. 手动编译websocket-sharp项目使其支持.net core
  5. Linux系统mysql使用(一)
  6. wordpress网站分类目录怎么排序
  7. vim设置行号
  8. NFS常见问题
  9. PHP最全笔记(五)(值得收藏,不时翻看一下)
  10. 百练-16年9月推免-C题-图像旋转
  11. Css学习(2)
  12. ASP.NET写的一个博客系统
  13. laravel 数据模型方法
  14. Unity3D笔记 英保通六 角色控制器
  15. 深入理解$watch ,$apply 和 $digest --- 理解数据绑定过程——续
  16. 函数数组demo
  17. springboot学习入门之三---启动原理
  18. Unit02: Servlet工作原理
  19. mybatis-plus 学习笔记
  20. Math对象及相关方法

热门文章

  1. CTF-OldDriver-writeup
  2. python mysql 类 图片保存到表中,从表中读图片形成图片文件
  3. 家庭账本开发day02
  4. 1.在配置XML文件时出现reference file contains errors (http://www.springframework.org/schema/beans/...解决方案
  5. ZooKeeper 分布式锁 Curator 源码 04:分布式信号量和互斥锁
  6. Leetcode8. 字符串转换整数 (atoi)
  7. Java 将Word转为Tiff
  8. sql语句优化原理
  9. Oracle导入dmp文件:ORACLE错误12899而拒绝行的问题如何解决
  10. 忘记oracle的sys和system的密码