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>
[紅色]必須是以下值中的一個:紅色、白色、藍色、綠色
</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>
</rule>
</grammar>