SemanticValue.ContainsKey(String) 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示当前 SemanticValue 实例集合是否包含具有给定的键字符串的子 SemanticValue 实例。
public:
 virtual bool ContainsKey(System::String ^ key);public bool ContainsKey(string key);abstract member ContainsKey : string -> bool
override this.ContainsKey : string -> boolPublic Function ContainsKey (key As String) As Boolean参数
- key
- String
包含String 用于标识 SemanticValue 子实例在当前 SemanticValue下的键字符串。
返回
返回 bool、true ,如果以字符串 key 标记的 SemanticValue 子实例被找到, 如果没有,返回 false。
实现
示例
以下示例演示事件的处理程序, SpeechRecognized 该处理程序旨在处理更改前景色和背景色的命令。
处理已识别但没有语义结构的短语后,处理程序使用 ContainsKey (applyChgToBackground、 colorRGBValueList或 colorStringList)检查是否存在适当的键,然后处理语义组织的数据。
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;
  };
注解
只能在运行时按键值访问数据,例如检查语义[“myKey”]。值,这会生成异常。 建议先使用 ContainsKey 查询 对象,然后再将 与 给定实例SemanticValue一起使用Item[]。