SrgsDocument 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 SrgsDocument 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| SrgsDocument() |
初始化 SrgsDocument 类的新实例。 |
| SrgsDocument(GrammarBuilder) |
从SrgsDocument对象初始化类的新实例GrammarBuilder。 |
| SrgsDocument(SrgsRule) |
初始化类的新实例 SrgsDocument ,并指定要 SrgsRule 成为语法的根规则的对象。 |
| SrgsDocument(String) |
初始化类的新实例,该实例 SrgsDocument 指定用于填充实例的 SrgsDocument XML 文档的位置。 |
| SrgsDocument(XmlReader) |
从引用 XML 格式语法文件的实例初始化类的新实例SrgsDocumentXmlReader。 |
注解
使用类的SrgsDocument构造函数,可以从包含 XML 格式语法路径的字符串或对象创建实例SrgsDocumentGrammarBuilderSrgsRuleXmlReader,也可以启动空白实例。SrgsDocument
SrgsDocument()
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
初始化 SrgsDocument 类的新实例。
public:
SrgsDocument();
public SrgsDocument();
Public Sub New ()
示例
以下示例创建一个ruleEurope和ruleSAmerica),每个规则都是一个包含三SrgsOneOf个SrgsItem对象的对象。 之后,将创建另一个SrgsOneOf对象,其中包含SrgsRuleRef引用和ruleEurope引用ruleSAmerica的对象。 然后,新 SrgsOneOf 对象将添加到 Elements 该对象的属性 winnerRule中。 之后,所有三个规则(winnerRule和ruleEuroperuleSAmerica)都会添加到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)
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
从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实例的对象。
例外
builder 是 null。
示例
以下示例使用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)
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
初始化类的新实例 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
对象root ruleSrgsDocument中的值。
例外
grammarRootRule 是 null。
示例
以下示例创建两个规则(chooseCities 和 destCities),用于选择航班的出发地和目的地城市。 该示例使用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);
注解
此构造函数将指定的规则添加到对象,SrgsRulesCollectionSrgsDocument并将其设置为Root语法规则。
适用于
SrgsDocument(String)
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
初始化类的新实例,该实例 SrgsDocument 指定用于填充实例的 SrgsDocument XML 文档的位置。
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 文件的位置。
例外
path 是 null。
path 是空字符串。
示例
以下示例从名为“srgsDocumentFile.xml”的文件创建新 SrgsDocument 文件。
string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");
SrgsDocument document = null;
if (File.Exists(srgsDocumentFile))
document = new SrgsDocument(srgsDocumentFile);
注解
重要
将此类型的实例用于不受信任的数据是一种安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅验证所有输入。
适用于
SrgsDocument(XmlReader)
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
- Source:
- SrgsDocument.cs
从引用 XML 格式语法文件的实例初始化类的新实例SrgsDocumentXmlReader。
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
XmlReader使用 SrgsDocument XML 实例创建的对象。
例外
srgsGrammar 是 null。
示例
以下示例从引用文件的实例创建一个新实例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();
}
注解
重要
将此类型的实例用于不受信任的数据是一种安全风险。 仅将此对象与受信任的数据一起使用。 有关详细信息,请参阅验证所有输入。