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

注解

通配符元素将添加到当前元素序列的末尾。

通配符元素与任何口述字词匹配。 它与背景噪音或静音不匹配。

适用于

另请参阅