Share via


SemanticResultValue.ToGrammarBuilder Methode

Definition

Gibt eine Instanz von GrammarBuilder zurück, die aus der aktuellen SemanticResultValue-Instanz konstruiert wurde.

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

Gibt zurück

Gibt eine Instanz von GrammarBuilder zurück, die aus der aktuellen SemanticResultValue-Instanz konstruiert wurde.

Beispiele

Im folgenden Beispiel werden Objekte erstellt Grammar , die Befehle zum Ändern der Hintergrundfarbe unterstützen.

Ein Choices Objekt (colorChoice), das die Liste der Optionen für Hintergrundfarben enthält, wird mithilfe der Add(GrammarBuilder[]) -Methode mit GrammarBuilder Instanzen gefüllt, die von der ToGrammarBuilder() -Methode für die SemanticResultValue aus Farbzeichenfolgen erstellten Objekte abgerufen werden.

Ein GrammarBuilder wird dann abgerufen, indem eine SemanticResultKey Instanz aufgerufen ToGrammarBuilder() wird, die verwendet wird, um die semantischen Optionen in der colorChoice Instanz zu schlüsseln.

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;
}

Hinweise

Die Verwendung von ToGrammarBuilder entspricht der Verwendung des Konstruktors, der GrammarBuilder ein SemanticResultValue als Argument (GrammarBuilder(SemanticResultValue)) annimmt.

Gilt für: