GrammarBuilder.AppendWildcard 方法

定義

將符合任何輸入的辨識文法項目附加至目前的文法項目序列。

public:
 void AppendWildcard();
public void AppendWildcard ();
member this.AppendWildcard : unit -> unit
Public Sub AppendWildcard ()

範例

下列範例會建立一個文法,以接受密碼輸入作為萬用字元。 此範例會將 Grammar.SpeechRecognized 事件處理常式附加至驗證密碼輸入的文法。

private Grammar CreatePasswordGrammar()
{
  GrammarBuilder wildcardBuilder = new GrammarBuilder();
  wildcardBuilder.AppendWildcard();
  SemanticResultKey passwordKey =
    new SemanticResultKey("Password", wildcardBuilder);

  GrammarBuilder passwordBuilder =
    new GrammarBuilder("My Password is");
  passwordBuilder.Append(passwordKey);

  Grammar passwordGrammar = new Grammar(passwordBuilder);
  passwordGrammar.Name = "Password input";

  passwordGrammar.SpeechRecognized +=
    new EventHandler<SpeechRecognizedEventArgs>(
      PasswordInputHandler);

  return passwordGrammar;
}

// Handle the SpeechRecognized event for the password grammar.
private void PasswordInputHandler(object sender, SpeechRecognizedEventArgs e)
{
  if (e.Result == null) return;

  RecognitionResult result = e.Result;
  SemanticValue semantics = e.Result.Semantics;

  if (semantics.ContainsKey("Password"))
  {
    RecognizedAudio passwordAudio =
      result.GetAudioForWordRange(
        result.Words[3], result.Words[result.Words.Count - 1]);

    if (IsValidPassword(passwordAudio))
    {
      Console.WriteLine("Password accepted.");

      // Add code to handle a valid password here.
    }
    else
    {
      Console.WriteLine("Invalid password.");

      // Add code to handle an invalid password here.
    }
  }
}

// Validate the password input.
private bool IsValidPassword(RecognizedAudio passwordAudio)
{
  Console.WriteLine("Validating password.");

  // Add password validation code here.

  return false;
}

備註

萬用字元專案會新增至目前專案序列的結尾。

萬用字元元素符合任何口語文字。 它不符合背景雜訊或無聲。

適用於

另請參閱