TextSource.GetTextRun(Int32) 方法

定义

检索从指定的 TextRun 位置处开始的 TextSource

public:
 abstract System::Windows::Media::TextFormatting::TextRun ^ GetTextRun(int textSourceCharacterIndex);
public abstract System.Windows.Media.TextFormatting.TextRun GetTextRun (int textSourceCharacterIndex);
abstract member GetTextRun : int -> System.Windows.Media.TextFormatting.TextRun
Public MustOverride Function GetTextRun (textSourceCharacterIndex As Integer) As TextRun

参数

textSourceCharacterIndex
Int32

指定 TextSource 中检索到 TextRun 的字符索引位置。

返回

TextRun

一个值,表示 TextRun 或派生自 TextRun 的对象。

示例

在以下示例中,将实现该方法的 GetTextRun 替代。

// Retrieve the next formatted text run for the text source.
public override TextRun GetTextRun(int textSourceCharacterIndex)
{
    // Determine whether the text source index is in bounds.
    if (textSourceCharacterIndex < 0)
    {
        throw new ArgumentOutOfRangeException("textSourceCharacterIndex", "Value must be greater than 0.");
    }

    // Determine whether the text source index has exceeded or equaled the text source length.
    if (textSourceCharacterIndex >= _text.Length)
    {
        // Return an end-of-paragraph indicator -- a TextEndOfParagraph object is a special type of text run.
        return new TextEndOfParagraph(1);
    }

    // Create and return a TextCharacters object, which is formatted according to
    // the current layout and rendering properties.
    if (textSourceCharacterIndex < _text.Length)
    {
        // The TextCharacters object is a special type of text run that contains formatted text.
        return new TextCharacters(
           _text,                                       // The text store
           textSourceCharacterIndex,                    // The text store index
           _text.Length - textSourceCharacterIndex,     // The text store length
           new CustomTextRunProperties());              // The layout and rendering properties
    }

    // Return an end-of-paragraph indicator if there is no more text source.
    return new TextEndOfParagraph(1);
}
' Retrieve the next formatted text run for the text source.
Public Overrides Function GetTextRun(ByVal textSourceCharacterIndex As Integer) As TextRun
    ' Determine whether the text source index is in bounds.
    If textSourceCharacterIndex < 0 Then
        Throw New ArgumentOutOfRangeException("textSourceCharacterIndex", "Value must be greater than 0.")
    End If

    ' Determine whether the text source index has exceeded or equaled the text source length.
    If textSourceCharacterIndex >= _text.Length Then
        ' Return an end-of-paragraph indicator -- a TextEndOfParagraph object is a special type of text run.
        Return New TextEndOfParagraph(1)
    End If

    ' Create and return a TextCharacters object, which is formatted according to
    ' the current layout and rendering properties.
    If textSourceCharacterIndex < _text.Length Then
        ' The TextCharacters object is a special type of text run that contains formatted text.
        Return New TextCharacters(_text, textSourceCharacterIndex, _text.Length - textSourceCharacterIndex, New CustomTextRunProperties()) ' The layout and rendering properties -  The text store length -  The text store index -  The text store
    End If

    ' Return an end-of-paragraph indicator if there is no more text source.
    Return New TextEndOfParagraph(1)
End Function

注解

文本运行是共享单个属性集的字符序列。 对格式的任何更改(如字体系列、字体样式、前景颜色、文本修饰或任何其他格式效果)会中断文本运行。 该 TextRun 类是类型层次结构的根,表示由处理的 TextFormatter多种文本内容类型。 派生自 TextRun 的每个类都表示非重复类型的文本内容。

实例 说明
TextRun 层次结构的根目录。 定义共享相同字符属性集的一组字符。
TextCharacters 从不同的物理字样定义字符字形的集合。
TextEmbeddedObject 定义一种文本内容类型,在该类型中,测量、命中测试和绘制整个内容作为不同的实体完成。 此类内容的一个示例是文本行中间的按钮。
TextEndOfLine 定义换行符代码。
TextEndOfParagraph 定义段落分隔字符代码。 派生自 TextEndOfLine
TextEndOfSegment 定义段中断标记。
TextHidden 定义一系列不可见字符。
TextModifier 定义修改范围的开头。

适用于