SemanticResultKey.ToGrammarBuilder Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Returnerar en instans av GrammarBuilder konstruerad från den aktuella SemanticResultKey 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
En instans av GrammarBuilder konstruerad från den aktuella SemanticResultKey instansen.
Exempel
I följande exempel skapas ett 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. Instanserna GrammarBuilder hämtas via ToGrammarBuilder() metoden på de objekt som SemanticResultValue skapats från färgsträngar.
En GrammarBuilder hämtas sedan genom att anropa ToGrammarBuilder() en SemanticResultKey instans som används för att ange de semantiska valen i 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;
}
Kommentarer
Användningen av ToGrammarBuilder motsvarar att använda GrammarBuilder konstruktorn som tar SemanticResultKey som argument (GrammarBuilder(SemanticResultKey)).