共用方式為


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)

適用於