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
    智能标记在段落中的开始位置。
  • propertyBag
    类型:Microsoft.Office.Interop.SmartTag.ISmartTagProperties
    一个属性包,包含标记的键/值对。可以为 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 安全性

请参见

参考

SmartTagRecognizeContext 接口

Microsoft.Office.Tools.Word 命名空间