SemanticValue.ContainsKey(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指出目前 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 其設計目的是要處理命令以變更前景和背景色彩。
處理已辨識但沒有語意結構的片語之後,處理常式會使用 ContainsKey (applyChgToBackground
、 colorRGBValueList
或 colorStringList)
來檢查是否有適當的索引鍵,然後處理語意組織的資料。
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;
};
備註
您只能在執行時間依索引鍵值存取資料,例如檢查 semantic[「myKey」]。值,這會產生例外狀況。 建議您先使用 查詢 物件 ContainsKey ,再搭配指定的 實例 SemanticValue 使用 Item[] 。