SemanticResultValue.ToGrammarBuilder 方法

定义

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

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

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

示例

下面的示例创建 Grammar 支持命令的对象来更改背景色。

一个 Choices 对象, colorChoice 其中包含用于背景色的选项的列表 () 使用 Add(GrammarBuilder[]) 与从 GrammarBuilder ToGrammarBuilder() 颜色字符串创建的对象上的方法获取的实例进行填充 SemanticResultValue

GrammarBuilder然后通过对实例调用来获取 ToGrammarBuilder() SemanticResultKey ,此实例将用于对实例中的语义选项进行关键 colorChoice

private Grammar CreateGrammarBuilderRGBSemantics()   
{  

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

    // Use implicit conversion of SemanticResultValue to GrammarBuilder.      
    SemanticResultValue colorValue =   
        new SemanticResultValue(colorName, Color.FromName(colorName).ToArgb());  
    colorChoice.Add(colorValue.ToGrammarBuilder());  
  }  

  SemanticResultKey choiceKey = new SemanticResultKey("rgb", colorChoice);  
  GrammarBuilder choiceBuilder = choiceKey.ToGrammarBuilder();  

  // Create two intermediate grammars with an 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 final grammar, which recognizes either intermediate grammar.  
  Grammar grammar = new Grammar(new Choices(new GrammarBuilder[] { makeBackgroundBuilder, configureBackgroundBuilder }));  
  grammar.Name = "Set Background Color";  

  return grammar;  
}  

注解

的使用 ToGrammarBuilder 等效于使用以 GrammarBuilder 作为参数的构造函数 SemanticResultValue (GrammarBuilder(SemanticResultValue)) 。

适用于