using System; using System.Collections.Generic; using System.Linq; using System.Speech.Recognition; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace TestWPFAnimation { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; } SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(); void MainWindow_Loaded(object sender, RoutedEventArgs e) { Choices preCmd = new Choices(); preCmd.Add(new string[] { "name", "age" }); GrammarBuilder gb = new GrammarBuilder(); gb.Append(preCmd); Grammar gr = new Grammar(gb); recEngine.LoadGrammarAsync(gr); recEngine.SetInputToDefaultAudioDevice(); recEngine.SpeechRecognized += recEngine_SpeechRecognized; } private void btnEnable_Click(object sender, RoutedEventArgs e) { recEngine.RecognizeAsync(RecognizeMode.Multiple); btnDisable.IsEnabled = true; } void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { switch (e.Result.Text) { case "name": txt.Text += "Darren"; break; case "age": txt.Text += "21"; break; default: txt.Text = e.Result.Text; break; } } private void btnDisable_Click(object sender, RoutedEventArgs e) { recEngine.RecognizeAsyncStop(); btnDisable.IsEnabled = false; } } }
参考:https://www.cnblogs.com/darrenji/p/4373664.html