SemanticValue.Item[String] 属性

定义

返回属于当前SemanticValue实例的子SemanticValue实例。

public:
 property System::Speech::Recognition::SemanticValue ^ default[System::String ^] { System::Speech::Recognition::SemanticValue ^ get(System::String ^ key); void set(System::String ^ key, System::Speech::Recognition::SemanticValue ^ value); };
public System.Speech.Recognition.SemanticValue this[string key] { get; set; }
member this.Item(string) : System.Speech.Recognition.SemanticValue with get, set
Default Public Property Item(key As String) As SemanticValue

参数

key
String

当前实例KeyValuePair<String, SemanticValue>中包含的键SemanticValue

属性值

返回作为键值对的一部分编制索引的当前 SemanticValue 子级: KeyValuePair<String,SemanticValue>

实现

例外

当前实例 SemanticValue 的子成员没有与参数匹配的 key 键。

尝试更改 SemanticValue 指定索引处的代码。

示例

以下示例演示用于处理命令以更改前景和背景色的事件的处理程序 SpeechRecognized

处理没有语义结构的已识别短语后,处理程序会检查 ContainsKey 是否存在适当的键(applyChgToBackgroundcolorRGBValueList,或 colorStringList),然后使用 Item[] 该属性获取具有所需信息的节点)。

下面突出显示了用法 Item[]

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 semantic 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 the foreground.
      if (ForeColor.GetBrightness() <= .50)
      {
        BackColor = Color.White;
      }
      else
      {
        BackColor = Color.Black;
      }
    }
    return;
  };

注解

只读 Item[] ,如果修改了成员,则会生成异常。

只能在运行时按键值访问数据,而不是在编译时访问数据,例如检查 semantic["myKey"].Value。 指定不存在的键将生成异常。

若要检测给定密钥的存在,请使用 ContainsKey 实例上的 SemanticValue 属性。

适用于