SmartTagRecognizeContext.PersistTag 方法
存放關於智慧標籤的資訊。
命名空間: Microsoft.Office.Tools.Word
組件: Microsoft.Office.Tools.Word (在 Microsoft.Office.Tools.Word.dll 中)
語法
'宣告
Sub PersistTag ( _
startIndex As Integer, _
length As Integer, _
propertyBag As ISmartTagProperties _
)
void PersistTag(
int startIndex,
int length,
ISmartTagProperties propertyBag
)
參數
- startIndex
型別:System.Int32
段落中智慧標籤的起始位置。
- length
型別:System.Int32
智慧標籤的長度。
- propertyBag
型別:Microsoft.Office.Interop.SmartTag.ISmartTagProperties
內含語彙基元 (Token) 索引鍵值組的屬性包。可以是 nullNull 參照 (即 Visual Basic 中的 Nothing)。
例外狀況
例外狀況 | 條件 |
---|---|
InvalidOperationException | PersistTag 不是從 Recognize 方法呼叫的。 |
備註
從 Recognize 方法的實作呼叫 PersistTag,表示在文字中找到智慧標籤。 使用 propertyBag 參數認可智慧標籤的任何自訂屬性。 您可以使用這些屬性,自訂在選取智慧標籤捷徑功能表項目時所採取的動作。
範例
下列程式碼範例示範如何從 Recognize 方法的實作呼叫 PersistTag。 這項實作會將每個智慧標籤詞彙與段落內容相互比較。 程式碼會針對段落中的每個智慧標籤詞彙分別加入自訂智慧標籤屬性,然後使用 PersistTag 方法辨認智慧標籤。 本範例假設您已從 [加入參考] 對話方塊的 [.NET] 索引標籤,新增 [Microsoft.Office.Interop.SmartTag] 的參考。 這個程式碼範例是 ISmartTagExtension 介面完整範例的一部分。
Private Sub Recognize(ByVal text As String,
ByVal site As ISmartTagRecognizerSite,
ByVal tokenList As ISmartTagTokenList,
ByVal context As SmartTagRecognizeContext) Implements ISmartTagExtension.Recognize
For Each term As String In smartTagDemo.Terms
' Search the text for the current smart tag term.
Dim index As Integer = text.IndexOf(term, 0)
While (index >= 0)
' Create a smart tag token and a property bag for the recognized term.
Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()
' Write a new property value.
Dim key As String = "Key1"
propertyBag.Write(key, DateTime.Now.ToString())
' Attach the smart tag to the term in the document
context.PersistTag(index, term.Length, propertyBag)
' Increment the index and then find the next instance of the smart tag term.
index += term.Length
index = text.IndexOf(term, index)
End While
Next
End Sub
void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList,
SmartTagRecognizeContext context)
{
foreach (string term in smartTagDemo.Terms)
{
// Search the text for the current smart tag term.
int index = text.IndexOf(term, 0);
while (index >= 0)
{
// Create a smart tag token and a property bag for the recognized term.
ISmartTagProperties propertyBag = site.GetNewPropertyBag();
// Write a new property value.
string key = "Key1";
propertyBag.Write(key, DateTime.Now.ToString());
// Attach the smart tag to the term in the document
context.PersistTag(index, term.Length, propertyBag);
// Increment the index and then find the next instance of the smart tag term.
index += term.Length;
index = text.IndexOf(term, index);
}
}
}
.NET Framework 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。