SemanticValue.Item Property
Provides read-only indexing child SemanticValue instances belonging to the current SemanticValue.
Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)
Syntax
'Declaration
Parameters
- key
A System.String containing a key for aKeyValuePair<String, SemanticValue>
contained in the current instance of SemanticValue.
Property Value
Returns an instance of SemanticValue that is a child of the current SemanticValue and indexable as part of a key value pair: KeyValuePair<String, SemanticValue>
.
Exceptions
Exception type | Condition |
---|---|
KeyNotFoundException | Thrown if no child member of the current instance of SemanticValue has the key matching the key parameter. |
InvalidOperationException | Thrown is code attempts to change the SemanticValue at a given index. |
Remarks
The Item is read-only and generates exceptions if members are modified.
Evaluating accessing by key value, for instance semantic["myKey"].Value, can only be done at run time, not at code computation. Specifying a key not present will generate an exception.
The presence of a given key can be found using the ContainsKey property on an SemanticValue instance.
Example
The example below shows a handler for a SpeechRecognized event designed to handle commands to change foreground and background color.
After dealing with the case of a phrase that is recognized, but has no semantic structure, the handler checks for the presence of appropriate keys applyChgToBackground
, colorRGBValueList
, or colorStringList
and used the Item property to obtain the nodes with needed information.
The use of Item is highlighted below.
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){
//This signature of recognition by grammar with no semantic
////We have to parse the string. hope last word is color, search for background or forground 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")) {
//Check background vs foreground
if (semantics.ContainsKey("applyChgToBackground")) {
changeBackGround = semantics["applyChgToBackground"].Value is bool;
}
//Get 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 {
//we have a semantics that does not contain the keys we support, throw an exception.
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 the 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 the text is readable regardless of Foreground.
if (ForeColor.GetBrightness() <= .50) {
BackColor = Color.White;
} else {
BackColor = Color.Black;
}
}
Return;
};
Thread Safety
All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.
Platforms
Development Platforms
Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition
Target Platforms
See Also
Reference
SemanticValue Class
SemanticValue Members
Microsoft.Speech.Recognition Namespace