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>

<Button Content="开始说话" Click="OnClick"/>

<TextBlock TextWrapping="Wrap" FontSize="20">

<Run>

请说出:把颜色改为[红色]

</Run>

<LineBreak/>

<Run>或者:把颜色设置为[红色]</Run>

<LineBreak/>

<Run>

[红色]必须是以下值中的一个:红色、白色、蓝色、绿色

</Run>

</TextBlock>

<TextBlock Name="tbRes" FontSize="24" TextWrapping="Wrap" Foreground="LightGreen"/>

</StackPanel>

</Page>

C#  code

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

public sealed partial class MainPage : Page

{

public MainPage()

{

this.InitializeComponent();

this.NavigationCacheMode = NavigationCacheMode.Required;

}

/// <summary>

/// Invoked when this page is about to be displayed in a Frame.

/// </summary>

/// <param name="e">Event data that describes how this page was reached.

/// This parameter is typically used to configure the page.</param>

protected override void OnNavigatedTo(NavigationEventArgs e)

{

// TODO: Prepare page for display here.

// TODO: If your application contains multiple pages, ensure that you are

// handling the hardware Back button by registering for the

// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.

// If you are using the NavigationHelper provided by some templates,

// this event is handled for you.

}

private async void OnClick(object sender, RoutedEventArgs e)

{

Button btn = sender as Button;

btn.IsEnabled = false;

using (SpeechRecognizer recognizer = new SpeechRecognizer())

{

try

{

// 加载语法文件

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

// 创建识别约束

SpeechRecognitionGrammarFileConstraint grammarfileConstraint = new SpeechRecognitionGrammarFileConstraint(sgrsFile);

// 加入到约束集合中

recognizer.Constraints.Add(grammarfileConstraint);

// 编译约束

SpeechRecognitionCompilationResult compilationResult = await recognizer.CompileConstraintsAsync();

if (compilationResult.Status == SpeechRecognitionResultStatus.Success)

{

// 开始识别

SpeechRecognitionResult result = await recognizer.RecognizeAsync();

if(result.Status == SpeechRecognitionResultStatus.Success)

{

tbRes.Text = string.Format("识别结果:{0}。", result.Text);

}

}

}

catch (Exception ex)

{

System.Diagnostics.Debug.WriteLine(ex.Message);

}

}

btn.IsEnabled = true;

}

}

约束文件   sgr.xml

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

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

<grammar xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0" version="1.0" xml:lang="zh-cn" root="color">

<rule id="color">

<item repeat="0-1">把</item>

<item>颜色</item>

<one-of>

<item>设置为</item>

<item>改为</item>

</one-of>

<one-of>

<item>红色</item>

<item>白色</item>

<item>绿色</item>

<item>蓝色</item>

</one-of>

</rule>

</grammar>

uwp 自定义语音识别规则的更多相关文章

  1. [uwp]自定义图形裁切控件

    开始之前,先上一张美图.图中的花叫什么,我已经忘了,或者说从来就不知道,总之谓之曰“野花”.只记得花很美,很香,春夏时节,漫山遍野全是她.这大概是七八年前的记忆了,不过她依旧会很准时的在山上沐浴春光, ...

  2. MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)

    前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...

  3. struts2 自定义校验规则

    自定义校验规则:(了解) 在Struts2自定义校验规则: 1.实现一个Validator 接口. 2.一般开发中继承ValidatorSupport 或者 FieldValidatorSupport ...

  4. yii2中自定义验证规则rules

    作者:白狼 出处:www.manks.top/article/yii2_custom_rules 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追 ...

  5. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  6. easyui的validatebox重写自定义验证规则的几个实例

    validatebox已经实现的几个规则: 验证规则是根据使用需求和验证类型属性来定义的,这些规则已经实现(easyui API): email:匹配E-Mail的正则表达式规则. url:匹配URL ...

  7. 自定义 Lint 规则简介

    上个月,笔者在巴黎 Droidcon 的 BarCamp 研讨会上聆听了 Matthew Compton 关于编写自己的 Lint 规则的讲话.深受启发之后,笔者想就此话题做进一步的探索. 定义 如果 ...

  8. 在ubuntu16.04中安装apache2+modsecurity以及自定义WAF规则详解

    一.Modsecurity规则语法示例 SecRule是ModSecurity主要的指令,用于创建安全规则.其基本语法如下: SecRule VARIABLES OPERATOR [ACTIONS] ...

  9. Scarpy 起始url 自定义代理 自定义去重规则

    - start_urls - 内部原理 """ scrapy引擎来爬虫中去起始的URL: 1. 调用start_requests并获取返回值 2. v = iter(返回 ...

随机推荐

  1. python 操作word

    pip install python.docx from docx import DocumentDoc = Document() 解释:from 从 docx这个文件中,导入一个叫Document的 ...

  2. vuejs第一集之:vuejs了解

    1,了解到前后端分离2,连接到vuejs3,搜集书籍: Vuejs前端开发基础与项目实战 (https://detail.tmall.com/item.htm?spm=a230r.1.14.107.6 ...

  3. Appium - adb monkey事件(二)

    操作事件简介 Monkey所执行的随机事件流中包含11大事件,分别是触摸事件.手势事件.二指缩放事件.轨迹事件.屏幕旋转事件.基本导航事件.主要导航事件.系统按键事件.启动Activity事件.键盘事 ...

  4. 祝贺|合肥.NET俱乐部第二期技术沙龙活动圆满成功

    热烈祝贺合肥.NET俱乐部第二期技术沙龙圆满成功,感恩参与活动的每一位小伙伴!正是因为有你们才促成了这次聚会的成功.现对此次活动进行简单回顾并附上精彩的活动图片,每一位参与活动者名单,以及此次活动讲师 ...

  5. Java 使用新方法打印Word文档

    前言 我曾写过一篇文章,它主要介绍了如何通过物理打印机和虚拟打印机来打印Word文档.今天这篇教程将介绍一种新的方法来实现对Word文档的打印. 此次使用到的类库仍然是Spire.Doc for Ja ...

  6. selenium 鼠标,键盘操作

    1.鼠标操作 导包:from selenium.webdriver.common.action_chains import ActionChains 1.context_click()        ...

  7. [HNOI]2011卡农

    这是一道很好的组合数学题. 对于和我一样五音里面有六音不全的人来说,我们就应该转换一下题目的意思: 一句话题意: 题目的意思就是说要从一个有 n 个元素的集合当中选出一个长度为m的集合,然后满足: 1 ...

  8. THE MINTO PYRAMID PRINCIPLE

    金字塔原理:(重点突出,逻辑清晰.层次分明,简单易懂的思考方式.沟通方式.规范的动作.) 结构:结论先行,以上统下,归类分组,逻辑递进.先重要后次要,先总结后具体,先框架后细节,先结论后原因,先结果后 ...

  9. 解决VS2017调试卡住的问题

    今天用VS2017调试程序时,程序没有按照预期的那样运行到断点处,并且结束调试时会卡很长时间. 那么解决方法是: 1. [Tools]-->[Options]-->[Debugging]- ...

  10. videojs文档翻译-EventTarget

    EventTarget new EventTarget()   EventTarget是一个可以与DOM EventTarget具有相同API的类. 它增加了包含冗长功能的缩写功能. 例如:on函数是 ...