次の方法で共有


SemanticValue.Item[String] プロパティ

定義

現在SemanticValueの に属する子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

パラメーター

key
String

SemanticValue の現在のインスタンスに含まれている KeyValuePair<String, SemanticValue> のキー。

プロパティ値

キー値ペアの一部としてインデックスを作成できる現在SemanticValueの の子を返します。 KeyValuePair<String,SemanticValue>

実装

例外

の現在のインスタンス SemanticValue の子メンバーに、 パラメーターに一致する key キーがありません。

コードは、指定したインデックスで を SemanticValue 変更しようとしました。

次の例は、前景色と背景色を SpeechRecognized 変更するコマンドを処理するように設計されたイベントのハンドラーを示しています。

セマンティック構造を持たない認識されたフレーズを処理した後、ハンドラーは (applyChgToBackground、、colorRGBValueListまたは colorStringList)) を使用してContainsKey適切なキーの存在を確認し、 プロパティをItem[]使用して必要な情報を持つノードを取得します。

の使用 Item[] を以下に強調表示します。

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

注釈

Item[]は読み取り専用であり、メンバーが変更されると例外が生成されます。

をチェックするなど、実行時にはキー値によってのみデータにsemantic["myKey"].Valueアクセスできます。コンパイル時にはアクセスできません。 存在しないキーを指定すると、例外が生成されます。

特定のキーの存在を検出するには、インスタンスで ContainsKey プロパティを SemanticValue 使用します。

適用対象