共用方式為


SrgsRule 建構函式

定義

初始化 SrgsRule 類別的新執行個體。

多載

SrgsRule(String)

初始化 SrgsRule 類別的新執行個體,並指定規則的識別項。

SrgsRule(String, SrgsElement[])

SrgsElement 物件的陣列,初始化 SrgsRule 類別的新執行個體。

SrgsRule(String)

來源:
SrgsRule.cs
來源:
SrgsRule.cs
來源:
SrgsRule.cs
來源:
SrgsRule.cs

初始化 SrgsRule 類別的新執行個體,並指定規則的識別項。

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

參數

id
String

規則的識別項。

例外狀況

idnull

id 是空的。

id 不是適當的規則識別項。

範例

下列範例會建立一個文法,可辨識「已贏得 World Cup 的一名」片語,後面接著一個贏得 World Cup 的國家/地區名稱。 此範例會 SrgsRule 建立名為 winnerRule 的物件,並將識別碼 WorldCupWinner 傳遞為 String 。 物件 SrgsOneOf 是由新的 SrgsItem 物件陣列所組成,其中包含規則要辨識的替代專案。

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;
}

備註

SrgsRule 構函式會 Id 初始化 屬性。 識別碼在指定的文法內必須是唯一的。

SrgsRule 構函式會在下列情況下擲回 FormatException

  • id 不是有效的 XML 名稱,如 EXTENSIble Markup Language (XML) 1.0 (Fifth Edition ) 中所定義。 若要解譯此定義,有效的 XML 名稱開頭必須是字母、底線 ('_') 或冒號 (':') ,而且後面可以接著零個或多個 NameChar 字元, (XML 規格) 中定義。

  • id 為 「Null」 或 「VOID」 或 「GARBAGE」。

  • id 至少包含一個不正確規則識別碼字元。 這些字元包括:'?'、'*'、'+'、'|'、' ('、') 、'^'、'$'、'/'、';'、'.'、'= <> '、''、'['、'['、']'、'{'、'}'、'\\'、'\t'、'\r'和 '\n'。

另請參閱

適用於

SrgsRule(String, SrgsElement[])

來源:
SrgsRule.cs
來源:
SrgsRule.cs
來源:
SrgsRule.cs
來源:
SrgsRule.cs

SrgsElement 物件的陣列,初始化 SrgsRule 類別的新執行個體。

public:
 SrgsRule(System::String ^ id, ... cli::array <System::Speech::Recognition::SrgsGrammar::SrgsElement ^> ^ elements);
public SrgsRule (string id, params System.Speech.Recognition.SrgsGrammar.SrgsElement[] elements);
new System.Speech.Recognition.SrgsGrammar.SrgsRule : string * System.Speech.Recognition.SrgsGrammar.SrgsElement[] -> System.Speech.Recognition.SrgsGrammar.SrgsRule
Public Sub New (id As String, ParamArray elements As SrgsElement())

參數

id
String

規則的識別項。

elements
SrgsElement[]

SrgsElement 項目的陣列。

例外狀況

idnull

elementsnull

id 是空的。

id 不是適當的規則識別項。

範例

下列範例會建立一個文法,可辨識「已贏得 World Cup 的一名」片語,後面接著一個贏得 World Cup 的國家/地區名稱。 此範例會建立名為 的 WorldCupWinner 公用規則。 然後,此範例會建立兩 SrgsRule 個 物件 , ruleEuroperuleSAmerica 傳入 String 規則識別碼的 ,以及 SrgsElement 包含 SrgsOneOf 物件的陣列。 接著,此範例會從規則 加入和傳回 ruleEuroperuleSAmerica 規則 WorldCupWinner 的參考。

public void WorldSoccerWinners ()
{
  // Create a grammar from an SRGSDocument, create a new rule
  // and set its scope to public.
  SrgsDocument srgsGrammar = 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 grammar and make winnerRule
  // the root rule of the grammar.
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});
  srgsGrammar.Root = winnerRule;
}

建立的文法具有下列格式。

<grammar version="1.0" xml:lang="en-US" xmlns="http://www.w3.org/2001/06/grammar" root="WorldCupWinner">
    <rule id="WorldCupWinner" scope="public">
        <item> A nation that has won the world cup is </item>
        <one-of>
            <item>
                <ruleref uri="#EuropeanNations" />
            </item>
            <item>
                <ruleref uri="#SouthAmericanNations" />
            </item>
        </one-of>
    </rule>
    <rule id="EuropeanNations">
        <one-of>
            <item> England </item>
            <item> France </item>
            <item> Germany </item>
            <item> Italy </item>
        </one-of>
    </rule>
    <rule id="SouthAmericanNations">
        <one-of>
            <item> Argentina </item>
            <item> Brazil </item>
            <item> Uruguay </item>
        </one-of>
    </rule>
</grammar>

備註

SrgsRule 構函式會 Id 初始化 屬性。 識別碼在指定的文法內必須是唯一的。

SrgsRule 構函式會在下列情況下擲回 FormatException

  • id 不是有效的 XML 名稱,如 EXTENSIble Markup Language (XML) 1.0 (Fifth Edition ) 中所定義。 若要解譯此定義,有效的 XML 名稱開頭必須是字母、底線 ('_') 或冒號 (':') ,而且後面可以接著零個或多個 NameChar 字元, (XML 規格) 中定義。

  • id 為 「Null」 或 「VOID」 或 「GARBAGE」。

  • id 至少包含一個不正確規則識別碼字元。 這些字元包括:'?'、'*'、'+'、'|'、' ('、') 、'^'、'$'、'/'、';'、'.'、'= <> '、''、'['、'['、']'、'{'、'}'、'\\'、'\t'、'\r'和 '\n'。

另請參閱

適用於