共用方式為


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

包含在 SemanticValue 目前執行個體中的 KeyValuePair<String, SemanticValue> 索引鍵。

屬性值

傳回目前 SemanticValue 的子系,其可編制索引做為索引鍵值組的一部分: KeyValuePair<String,SemanticValue>

實作

例外狀況

目前 實例 SemanticValue 沒有任何子成員具有符合 參數的 key 索引鍵。

程式碼嘗試在指定的索引處變更 SemanticValue

範例

下列範例顯示設計用來處理命令以變更前景和背景色彩之事件的處理常式 SpeechRecognized

處理沒有語意結構的辨識片語之後,處理常式會使用 ContainsKey (applyChgToBackgroundcolorRGBValueListcolorStringList) 檢查適當的索引鍵是否存在,然後使用 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 屬性。

適用於