SemanticValue.Item[String] Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Retourne des instances enfants SemanticValue qui appartiennent au fichier actif 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
Paramètres
- key
- String
Clé d’un KeyValuePair<String, SemanticValue> contenu dans l’instance actuelle de SemanticValue.
Valeur de propriété
Retourne un enfant du courant SemanticValue pouvant être indexé dans le cadre d’une paire clé-valeur : KeyValuePair<String,SemanticValue>.
Implémente
Exceptions
Aucun membre enfant de l’instance actuelle n’a SemanticValue de clé correspondant au key paramètre.
Le code a tenté de modifier l’index SemanticValue spécifié.
Exemples
L’exemple suivant montre un gestionnaire pour un SpeechRecognized événement conçu pour gérer les commandes afin de modifier le premier plan et la couleur d’arrière-plan.
Après avoir géré des expressions reconnues qui n’ont aucune structure sémantique, le gestionnaire vérifie la présence de clés appropriées à l’aide ContainsKey (applyChgToBackground, colorRGBValueListou , puis colorStringList)utilise la Item[] propriété pour obtenir les nœuds avec les informations nécessaires.
L’utilisation est Item[] mise en surbrillance ci-dessous.
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;
};
Remarques
Il Item[] est en lecture seule et génère des exceptions si les membres sont modifiés.
Vous pouvez uniquement accéder aux données par valeur de clé au moment de l’exécution, et non au moment de la compilation, par exemple pour vérifier semantic["myKey"].Value. La spécification d’une clé qui n’est pas présente génère une exception.
Pour détecter la présence d’une clé donnée, utilisez la ContainsKey propriété sur une SemanticValue instance.