SpeechRecognizer.SpeechRecognitionRejected 事件


当识别器接收不与它已加载的任何语音识别语法匹配的输入时发生。Occurs when the recognizer receives input that does not match any of the speech recognition grammars it has loaded.

 event EventHandler<System::Speech::Recognition::SpeechRecognitionRejectedEventArgs ^> ^ SpeechRecognitionRejected;
public event EventHandler<System.Speech.Recognition.SpeechRecognitionRejectedEventArgs> SpeechRecognitionRejected;
member this.SpeechRecognitionRejected : EventHandler<System.Speech.Recognition.SpeechRecognitionRejectedEventArgs> 
Public Event SpeechRecognitionRejected As EventHandler(Of SpeechRecognitionRejectedEventArgs) 


下面的示例将识别一些短语,如 "在爵士乐类别中显示音乐家列表" 或 "显示唱片集 gospel"。The following example recognizes phrases such as "Display the list of artists in the jazz category" or "Display albums gospel". 该示例使用SpeechRecognitionRejected事件的处理程序在控制台中显示一个通知,以使语音输入不能与语法的内容进行匹配,从而完全自信地生成成功的识别。The example uses a handler for the SpeechRecognitionRejected event to display a notification in the console when the speech input cannot be matched to the contents of the grammar with sufficient confidence to produce a successful recognition.

using System;  
using System.Speech.Recognition;  
namespace SampleRecognition  
  class Program  
    static void Main(string[] args)  
    // Initialize a shared speech recognition engine.  
      using (SpeechRecognizer recognizer =  
         new SpeechRecognizer())  
        // Create a grammar.  
        //  Create lists of alternative choices.  
        Choices listTypes = new Choices(new string[] { "albums", "artists" });  
        Choices genres = new Choices(new string[] {   
          "blues", "classical", "gospel", "jazz", "rock" });  
        //  Create a GrammarBuilder object and assemble the grammar components.  
        GrammarBuilder mediaMenu = new GrammarBuilder("Display");  
        mediaMenu.Append("the list of", 0, 1);  
        mediaMenu.Append("in the", 0, 1);  
        mediaMenu.Append("category", 0, 1);  
        //  Build a Grammar object from the GrammarBuilder.  
        Grammar mediaMenuGrammar = new Grammar(mediaMenu);  
        mediaMenuGrammar.Name = "Media Chooser";  
        // Attach event handlers.  
        recognizer.LoadGrammarCompleted +=  
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
        recognizer.SpeechRecognitionRejected +=   
          new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);  
        // Load the grammar object to the recognizer.  
        // Keep the console window open.  
    // Handle the SpeechRecognitionRejected event.  
    static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)  
      Console.WriteLine("Speech input was rejected.");  
    // Handle the LoadGrammarCompleted event.  
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
      Console.WriteLine("Grammar loaded: " + e.Grammar.Name);  
    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
      Console.WriteLine("Speech recognized: " + e.Result.Text);  


如果共享识别器确定输入与充分自信的任何已加载语音识别语法匹配,则会引发此事件。The shared recognizer raises this event if it determines that input does not match with sufficient confidence any of the loaded speech recognition grammars. RecognitionResultResult 属性包含被SpeechRecognitionRejectedEventArgs拒绝的对象。The Result property of the SpeechRecognitionRejectedEventArgs contains the rejected RecognitionResult object.

由管理SpeechRecognizer的共享识别器的置信度阈值与用户配置文件相关联,并存储在 Windows 注册表中。Confidence thresholds for the shared recognizer, managed by SpeechRecognizer, are associated with a user profile and stored in the Windows registry. 对于共享识别器的属性,应用程序不应将更改写入注册表。Applications should not write changes to the registry for the properties of the shared recognizer.

SpeechRecognitionRejected事件创建委托时,需要标识将处理该事件的方法。When you create a delegate for a SpeechRecognitionRejected event, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅事件和委托For more information about event-handler delegates, see Events and Delegates.