SemanticValue.ContainsKey(String) Méthode

Définition

Indique si la collection actuelle d'instance de SemanticValue contient une instance enfant de SemanticValue avec une chaîne clé donnée.

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

Paramètres

key
String

String contenant la chaîne de clé utilisée pour identifier une instance enfant de SemanticValue sous la SemanticValueactuelle.

Retours

Retourne bool, true si une instance enfant SemanticValue balisée avec la chaîne key est trouvée, false dans le cas contraire.

Implémente

Exemples

L’exemple suivant montre un gestionnaire pour un SpeechRecognized événement conçu pour gérer les commandes afin de modifier la couleur de premier plan et d’arrière-plan.

Après avoir géré des expressions qui sont reconnues mais qui n’ont pas de structure sémantique, le gestionnaire vérifie la présence de clés appropriées à l’aide ContainsKey de (applyChgToBackground, colorRGBValueListou colorStringList), ), puis traite les données sémantiquement organisées.

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

Remarques

Vous pouvez uniquement accéder aux données par valeur de clé au moment de l’exécution, par exemple pour case activée sémantique["myKey"]. Valeur, et cela génère une exception. Nous vous recommandons d’interroger l’objet avec ContainsKey avant d’utiliser Item[] avec un instance donné de SemanticValue.

S’applique à