SemanticValue.Item[String] Propiedad

Definición

Devuelve instancias de SemanticValue secundarias que pertenecen al SemanticValue actual.Returns child SemanticValue instances that belong to the current 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

Parámetros

key
String

Clave para KeyValuePair<String, SemanticValue> contenido en la instancia actual de SemanticValue.A key for a KeyValuePair<String, SemanticValue> contained in the current instance of SemanticValue.

Valor de propiedad

Devuelve un elemento secundario del actual SemanticValue que se puede indizar como parte de un par clave-valor: Cadena<KeyValuePair,SemanticValue>.Returns a child of the current SemanticValue that can be indexed as part of a key value pair: KeyValuePair<String,SemanticValue>.

Implementaciones

Excepciones

Se produce si ningún miembro secundario de la instancia actual de SemanticValue tiene una clave coincidente con el parámetro key.Thrown if no child member of the current instance of SemanticValue has the key matching the key parameter.

Se produce si el código intenta cambiar el SemanticValue en el índice especificado.Thrown if code attempts to change the SemanticValue at a given index.

Ejemplos

En el ejemplo siguiente se muestra un controlador SpeechRecognized para un evento diseñado para controlar los comandos para cambiar el color de primer plano y de fondo.The following example shows a handler for a SpeechRecognized event designed to handle commands to change foreground and background color.

Después de controlar frases reconocidas que no tienen una estructura semántica, el controlador comprueba la presencia de ContainsKey las claves colorRGBValueListadecuadas mediante colorStringList)(applyChgToBackground, o y, Item[String] a continuación, usa la propiedad para obtener los nodos. con la información necesaria.After handling recognized phrases that have no semantic structure, the handler checks for the presence of appropriate keys using ContainsKey (applyChgToBackground, colorRGBValueList, or colorStringList), and then uses the Item[String] property to obtain the nodes with needed information.

El uso de Item[String] se resalta a continuación.The use of Item[String] 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)  
      {  
        // 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;  
  };  

Comentarios

Item[String] Es de solo lectura y genera excepciones si se modifican los miembros.The Item[String] is read-only and generates exceptions if members are modified.

Solo puede obtener acceso a datos por valor de clave en tiempo de ejecución, no en tiempo de compilación, por ejemplo semantic["myKey"].Value, para comprobar.You can only access data by key value at run-time, not at compile-time, for example to check semantic["myKey"].Value. Al especificar una clave que no está presente se genera una excepción.Specifying a key that is not present generates an exception.

Para detectar la presencia de una clave determinada, use la ContainsKey propiedad en una SemanticValue instancia de.To detect the presence of a given key, use the ContainsKey property on an SemanticValue instance.

Se aplica a