SemanticValue.ContainsKey(String) Método

Definición

Indica si la colección actual de la instancia de SemanticValue contiene una instancia de SemanticValue secundaria con una cadena de clave especificada.Indicates whether the current SemanticValue instance collection contains a child SemanticValue instance with a given key string.

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

Parámetros

key
String

String que contiene la cadena de clave usada para identificar una instancia secundaria de SemanticValue bajo el SemanticValue actual.String containing the key string used to identify a child instance of SemanticValue under the current SemanticValue.

Devoluciones

Devuelve un valor bool, true si se encuentra una instancia secundaria de SemanticValue etiquetada con key de cadena; de lo contrario, es false.Returns a bool, true if a child instance SemanticValue tagged with the string key is found, false if not.

Implementaciones

Ejemplos

En el ejemplo siguiente se muestra un controlador SpeechRecognized para un evento diseñado para controlar los comandos para cambiar el color de primer plano y de fondo.The following example shows a handler for a SpeechRecognized event designed to handle commands to change foreground and background color.

Después de controlar frases que se reconocen pero no tienen una estructura semántica, el controlador comprueba la presencia de las claves ContainsKey adecuadasapplyChgToBackgroundmediante colorRGBValueList(, colorStringList)o y, a continuación, procesa los datos organizados semánticamente.After handling phrases that are recognized but have no semantic structure, the handler checks for the presence of appropriate keys using ContainsKey (applyChgToBackground, colorRGBValueList, or colorStringList), and then processes the semantically organized data.

  
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;  
  };  

Comentarios

Solo se puede tener acceso a los datos por valor de clave en tiempo de ejecución, por ejemplo, para comprobar la semántica ["mykey"]. Y esto genera una excepción.You can only access data by key value at runtime, for example to check semantic["myKey"].Value, and this generates an exception. Se recomienda consultar el objeto con ContainsKey antes de usar Item[String] con una instancia determinada de SemanticValue.We recommend that you query the object with ContainsKey before using Item[String] with a given instance of SemanticValue.

Se aplica a