SemanticValue.Item[String] Właściwość

Definicja

Zwraca wystąpienia podrzędne SemanticValue należące do bieżącego SemanticValueelementu .

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

Parametry

key
String

Klucz elementu KeyValuePair<String, SemanticValue> zawartego w bieżącym wystąpieniu klasy SemanticValue.

Wartość właściwości

Zwraca element podrzędny bieżącego SemanticValue elementu, który może być indeksowany jako część pary klucz-wartość: KeyValuePair<String,SemanticValue>.

Implementuje

Wyjątki

Żaden element podrzędny bieżącego wystąpienia programu SemanticValue nie ma klucza pasującego do parametru key .

Kod próbował zmienić wartość SemanticValue w określonym indeksie.

Przykłady

W poniższym przykładzie przedstawiono procedurę obsługi zdarzenia przeznaczonego SpeechRecognized do obsługi poleceń w celu zmiany koloru pierwszego planu i tła.

Po obsłudze rozpoznanych fraz, które nie mają struktury semantycznej, program obsługi sprawdza obecność odpowiednich kluczy przy użyciu (ContainsKeyapplyChgToBackground, colorRGBValueListlub colorStringList), a następnie używa Item[] właściwości w celu uzyskania węzłów z wymaganymi informacjami.

Użycie funkcji zostało wyróżnione Item[] poniżej.

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

Uwagi

Element Item[] jest tylko do odczytu i generuje wyjątki, jeśli elementy członkowskie są modyfikowane.

Dostęp do danych można uzyskać tylko według wartości klucza w czasie wykonywania, a nie w czasie kompilacji, na przykład w celu sprawdzenia semantic["myKey"].Value. Określenie klucza, który nie jest obecny, powoduje wygenerowanie wyjątku.

Aby wykryć obecność danego klucza, użyj ContainsKey właściwości w wystąpieniu SemanticValue .

Dotyczy