SemanticResultValue 构造函数  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 SemanticResultValue 类的新实例。
重载
| SemanticResultValue(Object) | 使用指定的语义值初始化 SemanticResultValue 类的新实例。 | 
| SemanticResultValue(GrammarBuilder, Object) | 使用 SemanticResultValue 对象初始化 GrammarBuilder 类和关联语义值的一个新实例。 | 
| SemanticResultValue(String, Object) | 使用 SemanticResultValue 对象初始化 String 类和关联语义值的一个新实例。 | 
注解
SemanticResultValue构造函数支持指定 Object 基础数据类型为、、或的 bool 实例 int float string 。
构造函数可以 SemanticResultValue 在以下两种情况中创建实例:
- SemanticResultValue当使用构造时,实例必须与语法元素显式关联 GrammarBuilder Grammar 。
- 已 - SemanticResultValue与一个字符串值短语或对象相关联 GrammarBuilder 。
SemanticResultValue(Object)
使用指定的语义值初始化 SemanticResultValue 类的新实例。
public:
 SemanticResultValue(System::Object ^ value);public SemanticResultValue (object value);new System.Speech.Recognition.SemanticResultValue : obj -> System.Speech.Recognition.SemanticResultValuePublic Sub New (value As Object)参数
- value
- Object
由 SemanticResultValue 托管的值。 必须是类型 bool、int、float,或 string。
示例
下面的示例返回一个 Grammar ,它可识别命令 "Set/Change/Alter 前台/Background ..." [color list] "。 SemanticResultValue 和 SemanticResultKey 实例 (结合使用 Choices 和 GrammarBuilder 对象) 用于定义可在识别时进行分析的语义。 分析的语义将确定请求的颜色,以及是要修改前景还是背景。
private Grammar FgBgColorGrammar()   
{  
  Grammar grammar = null;  
  // Allow the command to begin with set, alter, change.  
  Choices introChoices = new Choices();  
  foreach (string introString in new string[] { "Change", "Set", "Alter" })   
  {  
    GrammarBuilder introGB = new GrammarBuilder(introString);  
    introChoices.Add(  
                  new SemanticResultValue(introGB,  
                  String.Format("Command: {0}", introString)));  
  }           
  GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);  
  // Define the arguments for the command to select foreground or background   
  // and to change their color as semantic values.  
  Choices fgOrbgChoice = new Choices();  
  GrammarBuilder backgroundGB=new GrammarBuilder("background");  
  backgroundGB.Append(new SemanticResultValue(true));  
  fgOrbgChoice.Add(backgroundGB);  
  fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground", false));  
  SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgBool", fgOrbgChoice);  
  Choices colorChoice = new Choices();  
  foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))   
  {  
    // Use implicit conversion of SemanticResultValue to GrammarBuilder.      
    colorChoice.Add(  
                 (GrammarBuilder) (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));  
  }  
  // Create a GrammarBuilder for CmdArgs to be appended to CmdIntro using  
  // semantic keys.  
  GrammarBuilder cmdArgs = new GrammarBuilder();  
  cmdArgs.Append(new SemanticResultKey("BgOrFgBool", fgOrbgChoice));  
  cmdArgs.AppendWildcard();  
  cmdArgs.Append(new SemanticResultKey("colorStringList", colorChoice));  
  GrammarBuilder cmds =   
      GrammarBuilder.Add(  
        cmdIntro,   
        new GrammarBuilder(new SemanticResultKey("Cmd Args", cmdArgs)));  
  grammar = new Grammar(cmds);  
  grammar.Name = "Tree [Set,change,alter] [foreground,background] * color";  
  return grammar;  
}  
注解
SemanticResultValue此构造函数返回的不与任何特定语法元素关联。 必须通过将的实例与结合使用来使关联成为显式的 SemanticResultValue GrammarBuilder 。
例如,在下面的代码段中,如果 Grammar 使用此实例构造的构造了 " GrammarBuilder background" 一词,则的值 true 是在识别的短语语义中设置的。
GrammarBuilder backgroundGB=new GrammarBuilder("background");  
backgroundGB.Append(new SemanticResultValue(true));  
适用于
SemanticResultValue(GrammarBuilder, Object)
使用 SemanticResultValue 对象初始化 GrammarBuilder 类和关联语义值的一个新实例。
public:
 SemanticResultValue(System::Speech::Recognition::GrammarBuilder ^ builder, System::Object ^ value);public SemanticResultValue (System.Speech.Recognition.GrammarBuilder builder, object value);new System.Speech.Recognition.SemanticResultValue : System.Speech.Recognition.GrammarBuilder * obj -> System.Speech.Recognition.SemanticResultValuePublic Sub New (builder As GrammarBuilder, value As Object)参数
- builder
- GrammarBuilder
要用于识别的语法组件。
- value
- Object
由 SemanticResultValue 托管的值。 必须是类型 bool、int、float,或 string。
示例
下面的示例返回一个 Grammar ,它可识别命令 "Set/Change/Alter 前台/Background ..." [color list] "。 SemanticResultValue 和 SemanticResultKey 实例 (结合使用 Choices 和 GrammarBuilder 对象) 用于定义可在识别时进行分析的语义。 分析的语义将确定请求的颜色,以及是要修改前景还是背景。
private Grammar FgBgColorGrammar()   
{  
  Grammar grammar = null;  
  // Allow the command to begin with set, alter, change.  
  Choices introChoices = new Choices();  
  foreach (string introString in new string[] { "Change", "Set", "Alter" })   
  {  
    GrammarBuilder introGB = new GrammarBuilder(introString);  
    introChoices.Add(  
                  new SemanticResultValue(introGB,  
                  String.Format("Command: {0}", introString)));  
  }           
  GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);  
  // Define the arguments for the command to select foreground or background   
  // and to change their color as semantic values.  
  Choices fgOrbgChoice = new Choices();  
  GrammarBuilder backgroundGB=new GrammarBuilder("background");  
  backgroundGB.Append(new SemanticResultValue(true));  
  fgOrbgChoice.Add(backgroundGB);  
  fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground", false));  
  SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgBool", fgOrbgChoice);  
  Choices colorChoice = new Choices();  
  foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))   
  {  
    // Use implicit conversion of SemanticResultValue to GrammarBuilder.      
    colorChoice.Add(  
              (GrammarBuilder) (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));  
    }  
  // Create a GrammarBuilder for CmdArgs to be appended to CmdIntro using  
  // semantic keys.  
  GrammarBuilder cmdArgs = new GrammarBuilder();  
  cmdArgs.Append(new SemanticResultKey("BgOrFgBool", fgOrbgChoice));  
  cmdArgs.AppendWildcard();  
  cmdArgs.Append(new SemanticResultKey("colorStringList", colorChoice));  
  GrammarBuilder cmds =   
      GrammarBuilder.Add(  
                      cmdIntro,  
                      new GrammarBuilder(new SemanticResultKey("Cmd Args", cmdArgs)));  
  grammar = new Grammar(cmds);  
  grammar.Name = "Tree [Set,change,alter] [foreground,background] * color";  
  return grammar;  
}  
注解
如果在 GrammarBuilder 识别逻辑中使用指定的语法元素, value 将在识别的输出的语义中设置。
在下面的代码段中,如果使用实例构造的识别逻辑 GrammarBuilder (myGb) 使用 Choices 对象 (myChoice) 来标识输入,则将该值 true 添加到识别的语义。
myGb.Append(new SemanticResultValue(myChoice, true);  
As GrammarBuilder 支持、和的隐式转换, Choices  SemanticResultValue SemanticResultKey 此构造函数还可以使用这些对象。
适用于
SemanticResultValue(String, Object)
使用 SemanticResultValue 对象初始化 String 类和关联语义值的一个新实例。
public:
 SemanticResultValue(System::String ^ phrase, System::Object ^ value);public SemanticResultValue (string phrase, object value);new System.Speech.Recognition.SemanticResultValue : string * obj -> System.Speech.Recognition.SemanticResultValuePublic Sub New (phrase As String, value As Object)参数
- phrase
- String
要用于识别的短语。
- value
- Object
由 SemanticResultValue 托管的值。 必须是类型 bool、int、float,或 string。
示例
下面的示例返回一个 Grammar ,它可识别命令 "Set/Change/Alter 前台/Background ..." [color list] "。 SemanticResultValue 和 SemanticResultKey 实例 (结合使用 Choices 和 GrammarBuilder 对象) 用于定义可在识别时进行分析的语义。 分析的语义将确定请求的颜色,以及是要修改前景还是背景。
private Grammar FgBgColorGrammar()   
{  
  Grammar grammar = null;  
  // Allow command to begin with set, alter, change.  
  Choices introChoices = new Choices();  
  foreach (string introString in new string[] { "Change", "Set", "Alter" })   
  {  
    GrammarBuilder introGB = new GrammarBuilder(introString);  
    introChoices.Add(  
                  new SemanticResultValue(introGB,  
                  String.Format("Command: {0}", introString)));  
  }  
  GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);  
  // Define the arguments for the command to select foreground or background   
  // and to change their color as semantic values.  
  Choices fgOrbgChoice = new Choices();  
  GrammarBuilder backgroundGB=new GrammarBuilder("background");  
  backgroundGB.Append(new SemanticResultValue(true));  
  fgOrbgChoice.Add(backgroundGB);  
  fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground", false));  
  SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgBool", fgOrbgChoice);  
  Choices colorChoice = new Choices();  
  foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))   
  {  
    // Use implicit conversion of SemanticResultValue to GrammarBuilder.      
    colorChoice.Add(  
          (GrammarBuilder) (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));  
  }  
  // Create a GrammarBuilder for CmdArgs to be appended to CmdIntro using  
  // semantic keys.  
  GrammarBuilder cmdArgs = new GrammarBuilder();  
  cmdArgs.Append(new SemanticResultKey("BgOrFgBool", fgOrbgChoice));  
  cmdArgs.AppendWildcard();  
  cmdArgs.Append(new SemanticResultKey("colorStringList", colorChoice));  
  GrammarBuilder cmds =   
      GrammarBuilder.Add(cmdIntro,  
                         new GrammarBuilder(new SemanticResultKey("Cmd Args", cmdArgs)));  
  grammar = new Grammar(cmds);  
  grammar.Name = "Tree [Set,change,alter] [foreground,background] * color";  
  return grammar;  
}  
注解
如果在 phrase 识别逻辑中使用由指定的字符串,则 value 将在识别输出的语义中设置。
在以下代码段中,如果使用 GrammarBuilder 实例 () 构造的识别逻辑 myGb 使用字符串 "my 抵押" 来标识输入,则该值 true 将添加到识别的语义。
myGb.Append(new SemanticResultValue("my mortgage", true);