SemanticResultKey.ToGrammarBuilder 方法

定义

GrammarBuilder 实例返回 SemanticResultKey 结构的实例。

public:
 System::Speech::Recognition::GrammarBuilder ^ ToGrammarBuilder();
public System.Speech.Recognition.GrammarBuilder ToGrammarBuilder ();
member this.ToGrammarBuilder : unit -> System.Speech.Recognition.GrammarBuilder
Public Function ToGrammarBuilder () As GrammarBuilder

返回

GrammarBuilder

从当前 SemanticResultKey 实例构造的 GrammarBuilder 的实例。

示例

以下示例创建一个 Grammar 对象,该对象支持用于更改背景色的命令。

对象 Choices (colorChoice) 包含背景色选项列表的对象使用 Add(GrammarBuilder[]) 实例的方法 GrammarBuilder 填充。 这些 GrammarBuilder 实例通过 ToGrammarBuilder() 从颜色字符串创建的对象上 SemanticResultValue 的方法获取。

然后,通过调用SemanticResultKeyToGrammarBuilder()实例来获取 AGrammarBuilder,该实例将用于键式选项。colorChoice

private Grammar CreateGrammarBuilderRGBSemantics()   
{  

  // Create a set of choices, each a lookup from a color name to RGB.  
  // Choices constructors do not take SemanticResultValue parameters, so cast   
  // the SemanticResultValue to GrammarBuilder.  
  Choices colorChoice = new Choices();  
  foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))   
  {  
    SemanticResultValue colorValue=new SemanticResultValue(colorName, Color.FromName(colorName).ToArgb());  

    // Use implicit conversion of SemanticResultValue to GrammarBuilder.  
    colorChoice.Add(colorValue.ToGrammarBuilder());      
  }  
  SemanticResultKey choiceKey = new SemanticResultKey("rgb", colorChoice);  
  GrammarBuilder choiceBuilder = choiceKey.ToGrammarBuilder();  

  // Create two intermediate grammars with introductory phrase and the color choice.  
  GrammarBuilder makeBackgroundBuilder = "Make background";  
  makeBackgroundBuilder.Append(choiceBuilder);  

  GrammarBuilder configureBackgroundBuilder = new GrammarBuilder("Configure background as");  
  configureBackgroundBuilder.Append((new SemanticResultKey("rgb", colorChoice)).ToGrammarBuilder());  

  // Create the Grammar object, which recognizes either intermediate grammar.  
  Grammar grammar = new Grammar(new Choices(new GrammarBuilder[] {makeBackgroundBuilder, configureBackgroundBuilder}));  
  grammar.Name = "Make Background /Configure background as";  

  return grammar;  
}  

注解

ToGrammarBuilder 用法等效于使用 GrammarBuilder 用作 SemanticResultKey 参数的构造函数 (GrammarBuilder(SemanticResultKey)) 。

适用于