SemanticValue.Item[String] 属性

定义

返回属于当前 SemanticValue的子 SemanticValue 实例。Returns child SemanticValue instances that belong to the current 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

SemanticValue 的当前实例中包含 KeyValuePair<String, SemanticValue> 的密钥。A key for a KeyValuePair<String, SemanticValue> contained in the current instance of SemanticValue.

属性值

返回当前SemanticValue的可作为键值对的一部分进行索引的子级:KeyValuePair<String,SemanticValue>Returns a child of the current SemanticValue that can be indexed as part of a key value pair: KeyValuePair<String,SemanticValue>.

实现

异常

如果当前实例SemanticValue 的子成员没有与key 参数匹配 ,放弃安装。Thrown if no child member of the current instance of SemanticValue has the key matching the key parameter.

如果代码在给定索引下尝试更改 SemanticValue,放弃安装。Thrown if code attempts to change the SemanticValue at a given index.

示例

下面的示例演示了一个事件处理SpeechRecognized程序, 该事件旨在处理命令以更改前景色和背景色。The following example shows a handler for a SpeechRecognized event designed to handle commands to change foreground and background color.

处理没有语义结构的已识别的短语后, 处理程序使用ContainsKey (applyChgToBackgroundcolorRGBValueListcolorStringList)) 检查是否存在适当的键, 然后Item[String]使用属性获取节点。提供所需信息。After handling recognized phrases that have no semantic structure, the handler checks for the presence of appropriate keys using ContainsKey (applyChgToBackground, colorRGBValueList, or colorStringList), and then uses the Item[String] property to obtain the nodes with needed information.

下面突出显示Item[String]了的用法。The use of Item[String] is highlighted below.

  
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[String]只读的, 如果修改了成员, 则会生成异常。The Item[String] is read-only and generates exceptions if members are modified.

您只能在运行时通过键值 (而不是在编译时) 访问数据, 例如进行检查semantic["myKey"].ValueYou can only access data by key value at run-time, not at compile-time, for example to check semantic["myKey"].Value. 指定不存在的键会生成异常。Specifying a key that is not present generates an exception.

若要检测是否存在给定的键, 请ContainsKey SemanticValue在实例上使用属性。To detect the presence of a given key, use the ContainsKey property on an SemanticValue instance.

适用于