SemanticValue.Count 属性

定义

返回当前 SemanticValue 实例下子 SemanticValue 对象的数目。

public:
 property int Count { int get(); };
public int Count { get; }
member this.Count : int
Public ReadOnly Property Count As Integer

属性值

返回当前 SemanticValue 实例下的子 SemanticValue 对象的数目。

实现

示例

以下示例演示事件的处理程序, SpeechRecognized 该处理程序旨在处理更改前景色和背景色的命令。

处理程序通过检测 零 的 和 CountValuenull来标识没有基础语义结构的已识别短语。 然后,通过分析原始文本直接处理此识别输出。

在其他情况下,处理程序使用键获取颜色名称的 RGB 分量,以确定命令是更改前景还是背景,或者指示找不到有效键。

newGrammar.SpeechRecognized +=
  delegate(object sender, SpeechRecognizedEventArgs eventArgs)
  {

    // Retrieve the value of the semantic property.
    bool changeBackGround = true;
    string errorString = "";
    SemanticValue semantics = eventArgs.Result.Semantics;

    Color newColor = Color.Empty;

    try
    {
      if (semantics.Count == 0 && semantics.Value==null)
      {
        // Signifies recognition by a grammar with no semantics.
        // Parse the string, assuming that the last word is color,
        //  searching for background or foreground in input.
        if (eventArgs.Result.Text.Contains("foreground"))
        {
          changeBackGround = false;
        }
        string cName = eventArgs.Result.Words[eventArgs.Result.Words.Count - 1].Text;
        newColor = Color.FromName(cName);

      }
      else if (semantics.ContainsKey("colorStringList") ^ semantics.ContainsKey("colorRGBValueList"))
      {

        // Determine whether to change background or foreground.
        if (semantics.ContainsKey("applyChgToBackground"))
        {
          changeBackGround = semantics["applyChgToBackground"].Value is bool;
        }

        // Get the RGB color value.
        if (semantics.ContainsKey("colorStringList"))
        {
          newColor = Color.FromName((string)semantics["colorStringList"].Value);
        }
        if (semantics.ContainsKey("colorRGBValueList"))
        {
          newColor = System.Drawing.Color.FromArgb((int)semantics["colorRGBValueList"].Value);
        }
      }
      else
      {

        // Throw an exception if the semantics do not contain the keys we
        // support.
        throw(new Exception("Unsupported semantics keys found."));
      }
    }

    catch (Exception exp)
    {
      MessageBox.Show(String.Format("Unable to process color semantics.:\n{0}\n", exp.Message));
      return;
    }

    // Change colors, either foreground or background.
    if (changeBackGround)
    {
      BackColor = newColor;
      float Bright = BackColor.GetBrightness();
      float Hue = BackColor.GetHue();
      float Sat = BackColor.GetSaturation();
      // Make sure that text is readable regardless of background.
      if (BackColor.GetBrightness() <= .50)
      {
        ForeColor = Color.White;
      }
      else
      {
        ForeColor = Color.Black;
      }
    }
    else
    {
      ForeColor = newColor;
      float Bright = ForeColor.GetBrightness();
      float Hue = ForeColor.GetHue();
      float Sat = ForeColor.GetSaturation();

      // Make sure that text is readable regardless of Foreground.
      if (ForeColor.GetBrightness() <= .50)
      {
        BackColor = Color.White;
      }
      else
      {
        BackColor = Color.Black;
      }
    }
    return;
  };

注解

不使用语义分析的识别结果的值始终为 Count 零,值为 Valuenull

适用于