SmartTagRecognizeContext.PersistTag, méthode

Stocke des informations relatives à la balise active.

Espace de noms :  Microsoft.Office.Tools.Word
Assembly :  Microsoft.Office.Tools.Word (dans Microsoft.Office.Tools.Word.dll)

Syntaxe

'Déclaration
Sub PersistTag ( _
    startIndex As Integer, _
    length As Integer, _
    propertyBag As ISmartTagProperties _
)
void PersistTag(
    int startIndex,
    int length,
    ISmartTagProperties propertyBag
)

Paramètres

  • startIndex
    Type : System.Int32
    Position dans le paragraphe où commence la balise active.
  • propertyBag
    Type : Microsoft.Office.Interop.SmartTag.ISmartTagProperties
    Sac de propriété qui contient les paires valeur-clé du jeton.Peut être nullune référence null (Nothing en Visual Basic).

Exceptions

Exception Condition
InvalidOperationException

PersistTag n'a pas été appelé à partir de la méthode Recognize.

Notes

Appelez PersistTag à partir d'une implémentation de la méthode Recognize pour indiquer que la balise active a été trouvée dans le texte. Utilisez le paramètre propertyBag pour valider toute propriété personnalisée pour la balise active. Vous pouvez utiliser ces propriétés pour personnaliser l'action effectuée lorsqu'un élément du menu contextuel de la balise active est sélectionné.

Exemples

L'exemple de code suivant montre comment appeler PersistTag à partir d'une implémentation de la méthode Recognize. Cette implémentation compare chaque terme de balise active au contenu du paragraphe. Pour chaque terme de la balise active trouvé dans le paragraphe, le code ajoute une propriété de balise active personnalisée et utilise la méthode PersistTag pour reconnaître la balise active. Cet exemple suppose que vous avez ajouté une référence à Microsoft.Office.Interop.SmartTag à partir de l'onglet .NET de la boîte de dialogue Ajouter une référence. Cet exemple de code fait partie d'un exemple plus complet fourni pour l'interface 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);
        }
    }
}

Sécurité .NET Framework

Voir aussi

Référence

SmartTagRecognizeContext Interface

Microsoft.Office.Tools.Word, espace de noms