SrgsDocument 构造函数

定义

初始化 SrgsDocument 类的新实例。

重载

SrgsDocument()

初始化 SrgsDocument 类的新实例。

SrgsDocument(GrammarBuilder)

SrgsDocument 对象初始化 GrammarBuilder 类的新实例。

SrgsDocument(SrgsRule)

初始化 SrgsDocument 类的新实例并指定一个 SrgsRule 对象成为语法的根规则。

SrgsDocument(String)

初始化指定填充 SrgsDocument 实例的 XML 文档的位置的 SrgsDocument 类的新实例。

SrgsDocument(XmlReader)

初始化 SrgsDocument 类的新实例,该类来自引用 XML 格式语法文件的 XmlReader 的实例。

注解

通过使用类的构造函数 SrgsDocument ,您可以从 SrgsDocument 一个 GrammarBuilder 、或对象创建一个 SrgsRule XmlReader 包含 XML 格式语法路径的字符串,也可以启动的空白实例 SrgsDocument

SrgsDocument()

初始化 SrgsDocument 类的新实例。

public:
 SrgsDocument();
public SrgsDocument ();
Public Sub New ()

示例

下面的示例创建一个 SrgsDocument 对象,然后创建一个名为的公共规则 winnerRule 。 然后创建一个 SrgsItem ,它包含字符串 "已赢得世界杯的国家/地区",并将此项添加到规则的属性中 Elements 。 然后,该示例创建两个规则 (ruleEuroperuleSAmerica) ,其中每个规则都是 SrgsOneOf 包含三个对象的对象 SrgsItem 。 然后,创建另一个 SrgsOneOf 对象,该对象包含 SrgsRuleRef 引用和的 ruleEurope 对象 ruleSAmerica 。 然后,新的 SrgsOneOf 对象将添加到的 Elements 属性中 winnerRule 。 此后,所有三个规则 (winnerRuleruleEuroperuleSAmerica) 都添加到的 Rules 属性中 SrgsDocument 。 最后,三个规则编译为语法的二进制表示形式。

public void WorldSoccerWinners ()  
{  

  // Create an SrgsDocument, create a new rule  
  // and set its scope to public.  
  SrgsDocument document = new SrgsDocument();  
  SrgsRule winnerRule = new SrgsRule("WorldCupWinner");  
  winnerRule.Scope = SrgsRuleScope.Public;  

  // Add the introduction.  
  winnerRule.Elements.Add(new SrgsItem("A nation that has won the World Cup is: "));  

  // Create the rule for the European nations.  
  SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"),   
    new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});  
  SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));  

  // Create the rule for the South American nations.  
  SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"),   
    new SrgsItem("Brazil"), new SrgsItem("Uruguay")});  
  SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));  

  // Add references to winnerRule for ruleEurope and ruleSAmerica.  
  winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem   
    (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));  

  // Add all the rules to the document and make winnerRule   
  // the root rule of the document.  
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});  
  document.Root = winnerRule;  

  String fileName = Path.GetTempFileName();  
  using (FileStream stream = new FileStream(fileName, FileMode.Create))   
  {   

    // Compile the grammar to a binary format.  
    SrgsGrammarCompiler.Compile(document, stream);   
  }  
}  

注解

此构造函数创建一个空的 SrgsDocument 实例。 若要在空的实例中生成语法 SrgsDocument ,请添加表示 SRGS 元素的类的实例,例如 SrgsRule 、、 SrgsRuleRef SrgsOneOfSrgsItem

适用于

SrgsDocument(GrammarBuilder)

SrgsDocument 对象初始化 GrammarBuilder 类的新实例。

public:
 SrgsDocument(System::Speech::Recognition::GrammarBuilder ^ builder);
public SrgsDocument (System.Speech.Recognition.GrammarBuilder builder);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (builder As GrammarBuilder)

参数

builder
GrammarBuilder

用于创建此 GrammarBuilder 实例的 SrgsDocument 对象。

例外

buildernull

示例

下面的示例 GrammarBuilder 使用对象在实例中生成语法 Choices 。 然后,它 SrgsDocument 从对象创建 GrammarBuilder

GrammarBuilder builder = null;  

// Create new Choices objects and add countries/regions, and create GrammarBuilder objects.  
Choices choicesEurope = new Choices(new string[] { "England", "France", "Germany", "Italy" });  
GrammarBuilder europe = new GrammarBuilder(choicesEurope);  

Choices choicesSAmerica = new Choices(new string[] { "Argentina", "Brazil", "Uruguay" });  
GrammarBuilder sAmerica = new GrammarBuilder(choicesSAmerica);  

Choices worldCupWinnerChoices = new Choices(new GrammarBuilder[] {choicesEurope, choicesSAmerica});  

// Create new GrammarBuilder from a Choices object.  
builder = new GrammarBuilder(worldCupWinnerChoices);  

// Create an SrgsDocument object from a GrammarBuilder object.  
SrgsDocument document = new SrgsDocument(builder);  

适用于

SrgsDocument(SrgsRule)

初始化 SrgsDocument 类的新实例并指定一个 SrgsRule 对象成为语法的根规则。

public:
 SrgsDocument(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ grammarRootRule);
public SrgsDocument (System.Speech.Recognition.SrgsGrammar.SrgsRule grammarRootRule);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : System.Speech.Recognition.SrgsGrammar.SrgsRule -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (grammarRootRule As SrgsRule)

参数

grammarRootRule
SrgsRule

SrgsDocument 对象中的 root rule

例外

grammarRootRulenull

示例

下面的示例创建两个规则 (chooseCitiesdestCities) 为航班选择源和目标城市。 此示例 SrgsDocument 使用 chooseCities 规则作为参数来初始化实例。 该示例将规则集合的内容和根规则的名称写入控制台。

// Create a rule that contains a list of destination cities.  
SrgsRule destCities = new SrgsRule("Destination");  
SrgsOneOf toCities = new SrgsOneOf(new string[] { "New York", "Seattle", "Denver" });  
destCities.Add(toCities);  

// Create a list of origin cities and supporting phrases.  
SrgsOneOf fromCities = new SrgsOneOf(new SrgsItem[] {   
  new SrgsItem("Dallas"), new SrgsItem("Miami"), new SrgsItem("Chicago") });  
SrgsItem intro = new SrgsItem("I want to fly from");  
SrgsItem to = new SrgsItem("to");  

// Create the root rule of the grammar, and assemble the components.  
SrgsRule chooseCities = new SrgsRule("Trip");  
chooseCities.Add(intro);  
chooseCities.Add(fromCities);  
chooseCities.Add(to);  
chooseCities.Add(new SrgsRuleRef(destCities));  

// Create the SrgsDocument and specify the root rule to add.  
SrgsDocument bookFlight = new SrgsDocument(chooseCities);  

// Add the rule for the destination cities to the document's rule collection.  
bookFlight.Rules.Add(new SrgsRule[] { destCities });  

// Display the contents of the Rules collection and the name of the root rule.  
foreach (SrgsRule rule in bookFlight.Rules)  
{  
  Console.WriteLine("Rule " + rule.Id + " is in the rules collection");  
}  
Console.WriteLine("Root Rule " + bookFlight.Root.Id);  

// Create a Grammar object and load it to the recognizer.  
Grammar g = new Grammar(bookFlight);  
g.Name = ("City Chooser");  
recognizer.LoadGrammarAsync(g);  

注解

此构造函数将指定规则添加到 SrgsRulesCollection 对象的, SrgsDocument 并将其设置为 Root 语法规则。

适用于

SrgsDocument(String)

初始化指定填充 SrgsDocument 实例的 XML 文档的位置的 SrgsDocument 类的新实例。

public:
 SrgsDocument(System::String ^ path);
public SrgsDocument (string path);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : string -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (path As String)

参数

path
String

SRGS XML 文件的位置。

例外

pathnull

path 是一个空字符串。

示例

下面的示例 SrgsDocument 从名为 "srgsDocumentFile.xml" 的文件中创建一个新的。

string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");  
SrgsDocument document = null;  

if (File.Exists(srgsDocumentFile))  
   document = new SrgsDocument(srgsDocumentFile);  

适用于

SrgsDocument(XmlReader)

初始化 SrgsDocument 类的新实例,该类来自引用 XML 格式语法文件的 XmlReader 的实例。

public:
 SrgsDocument(System::Xml::XmlReader ^ srgsGrammar);
public SrgsDocument (System.Xml.XmlReader srgsGrammar);
new System.Speech.Recognition.SrgsGrammar.SrgsDocument : System.Xml.XmlReader -> System.Speech.Recognition.SrgsGrammar.SrgsDocument
Public Sub New (srgsGrammar As XmlReader)

参数

srgsGrammar
XmlReader

与 XML XmlReader 实例创建的 SrgsDocument 对象。

例外

srgsGrammarnull

示例

下面的示例 SrgsDocumentXmlReader 引用文件 "srgsDocumentFile.xml" 的实例创建的一个新实例。

string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");  
SrgsDocument document = null;  

if (File.Exists(srgsDocumentFile))  
{  
  XmlReader reader = XmlReader.Create(srgsDocumentFile);  
  document = new SrgsDocument(reader);  
  reader.Close();  
}  

适用于