SemanticResultValue.ToGrammarBuilder Metod

Definition

Returnerar en instans av GrammarBuilder konstruerad från den aktuella SemanticResultValue instansen.

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

Returer

Returnerar en instans av GrammarBuilder konstruerad från den aktuella SemanticResultValue instansen.

Exempel

I följande exempel skapas Grammar objekt som stöder kommandon för att ändra bakgrundsfärgen.

Ett Choices objekt (colorChoice) som innehåller listan med alternativ för bakgrundsfärger fylls med metoden Add(GrammarBuilder[]) med GrammarBuilder instanser som hämtas från ToGrammarBuilder() metoden på de SemanticResultValue objekt som skapats från färgsträngar.

A GrammarBuilder hämtas sedan genom att anropa ToGrammarBuilder() en SemanticResultKey instans, som används för att ange de semantiska valen i instansen 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;
}

Kommentarer

Användningen av ToGrammarBuilder motsvarar att använda GrammarBuilder konstruktorn som tar ett SemanticResultValue som argument (GrammarBuilder(SemanticResultValue)).

Gäller för