TextPointer.IsInSameDocument(TextPointer) 方法

定义

指示指定位置与当前位置是否位于相同的文本容器内。

public:
 bool IsInSameDocument(System::Windows::Documents::TextPointer ^ textPosition);
public bool IsInSameDocument (System.Windows.Documents.TextPointer textPosition);
member this.IsInSameDocument : System.Windows.Documents.TextPointer -> bool
Public Function IsInSameDocument (textPosition As TextPointer) As Boolean

参数

textPosition
TextPointer

一个 TextPointer,它指定要与当前位置进行比较的位置。

返回

Boolean

如果 textPosition 指示的位置与当前位置位于相同的文本容器内,则为 true;否则为 false

例外

textPositionnull

示例

以下示例演示了此方法的用法。 该示例使用该方法IsInSameDocument检查指定是否放置在另外两个指定TextPointerTextPointer实例之间,在这种情况下,无法保证这三个位置都属于同一文本容器。

// This method first checks for compatible text container scope, and then checks whether
// a specified position is between two other specified positions.
bool IsPositionContainedBetween(TextPointer positionToTest, TextPointer start, TextPointer end)
{
    // Note that without this check, an exception will be raised by CompareTo if positionToTest 
    // does not point to a position that is in the same text container used by start and end.
    //
    // This test also implicitely indicates whether start and end share a common text container.
    if (!positionToTest.IsInSameDocument(start) || !positionToTest.IsInSameDocument(end)) 
        return false;
    
    return start.CompareTo(positionToTest) <= 0 && positionToTest.CompareTo(end) <= 0;
}
' This method first checks for compatible text container scope, and then checks whether
' a specified position is between two other specified positions.
Private Function IsPositionContainedBetween(ByVal positionToTest As TextPointer, ByVal start As TextPointer, ByVal [end] As TextPointer) As Boolean
    ' Note that without this check, an exception will be raised by CompareTo if positionToTest 
    ' does not point to a position that is in the same text container used by start and end.
    '
    ' This test also implicitely indicates whether start and end share a common text container.
    If (Not positionToTest.IsInSameDocument(start)) OrElse (Not positionToTest.IsInSameDocument([end])) Then
        Return False
    End If

    Return start.CompareTo(positionToTest) <= 0 AndAlso positionToTest.CompareTo([end]) <= 0
End Function

注解

涉及多个 TextPointer 实例的大多数操作仅在有问题的实例指示位于同一文本容器作用域中的位置时有效。 例如, CompareTo 不能将和 GetOffsetToPosition 方法与与当前位置关联的文本容器外部的位置一起使用 TextPointer 。 使用此方法验证指定 TextPointer 是否与此类操作的当前位置兼容。

适用于