SrgsDocument 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 SrgsDocument 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| SrgsDocument() |
初始化 SrgsDocument 類別的新執行個體。 |
| SrgsDocument(GrammarBuilder) |
初始化該類別的新實例 SrgsDocument ,從物件 GrammarBuilder 中初始化。 |
| SrgsDocument(SrgsRule) |
初始化該類別的新實例 SrgsDocument ,並指定一個 SrgsRule 物件作為文法的根規則。 |
| SrgsDocument(String) |
初始化該類別的新實例 SrgsDocument ,指定用於填補實 SrgsDocument 例的 XML 文件位置。 |
| SrgsDocument(XmlReader) |
從參考 XML 格式文法檔案的實例SrgsDocument初始化該XmlReader類別的新實例。 |
備註
利用類別的SrgsDocument構造子,你可以從SrgsDocument包含 XML 格式文法路徑的字串,從 、 GrammarBuilder或SrgsRule物件建立一個XmlReader實例,或者你也可以啟動一個空白實例SrgsDocument。
SrgsDocument()
初始化 SrgsDocument 類別的新執行個體。
public:
SrgsDocument();
public SrgsDocument();
Public Sub New ()
範例
以下範例建立一個 SrgsDocument 物件,然後建立一個名為 winnerRule的公開規則。 接著它會產生 SrgsItem 一個由字串「A nation who had won the World Cup」組成的 ,並將此項目加入 Elements 規則的屬性中。 接著範例會產生兩條規則(ruleEurope 和 ruleSAmerica),每個規則都包含 SrgsOneOf 三個 SrgsItem 物件。 之後,會建立另一個 SrgsOneOf 物件,包含 SrgsRuleRef 指向 ruleEurope 和 ruleSAmerica的物件。 接著將新 SrgsOneOf 物件加入 Elements 的 winnerRule性質中。 之後,將三條規則winnerRule(、 ruleEurope和 ruleSAmerica)全部加入 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例的物件。
例外狀況
builder 是 null。
範例
以下範例在實GrammarBuilderChoices例中使用物件建構文法。 接著它從物件中產生一個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
在物品裡root rule。SrgsDocument
例外狀況
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);
備註
此建構子將指定的規則加入物件的 SrgsRulesCollection ,SrgsDocument並將其設為Root文法的規則。
適用於
SrgsDocument(String)
初始化該類別的新實例 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);
備註
Important
使用這種類型的實例與不受信任的資料存在安全風險。 此物件僅用於受信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。
適用於
SrgsDocument(XmlReader)
從參考 XML 格式文法檔案的實例SrgsDocument初始化該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
是 XmlReader 用 SrgsDocument XML 實例建立的物件。
例外狀況
srgsGrammar 是 null。
範例
以下範例從一個引用檔案「srgsDocumentFile.xml」的實例SrgsDocument建立一個XmlReader新的實例。
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();
}
備註
Important
使用這種類型的實例與不受信任的資料存在安全風險。 此物件僅用於受信任的資料。 如需詳細資訊,請參閱 驗證所有輸入。