SemanticValue.ContainsKey(String) 메서드

정의

현재 SemanticValue 인스턴스 컬렉션이 지정된 키 문자열과 함께 자식 SemanticValue 인스턴스를 포함하는지 여부를 나타냅니다.

public:
 virtual bool ContainsKey(System::String ^ key);
public bool ContainsKey (string key);
abstract member ContainsKey : string -> bool
override this.ContainsKey : string -> bool
Public Function ContainsKey (key As String) As Boolean

매개 변수

key
String

현재 String에서 SemanticValue의 자식 인스턴스를 식별하는데 사용하는 키 문자열이 포함된 SemanticValue

반환

bool이 반환됩니다. key가 발견된 문자열과 태그된 자식 인스턴스 SemanticValue인 경우 true이고, 그렇지 않으면 false 입니다.

구현

예제

다음 예제에 대 한 처리기를 SpeechRecognized 이벤트 전경색과 배경색을 변경 하는 명령 처리 하도록 설계 되었습니다.

인식되지만 의미 체계 구조가 없는 구를 처리한 후 처리기는 (, colorRGBValueList또는 colorStringList))applyChgToBackground를 사용하여 ContainsKey 적절한 키의 존재를 확인한 다음 의미 체계로 구성된 데이터를 처리합니다.

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 사용 하기 전에 Item[] 의 특정된 인스턴스와 함께 SemanticValue입니다.

적용 대상