UWP RichEditBox相当于GetCharIndexFromPosition

时间:2017-03-27 17:45:35

标签: c# xaml uwp uwp-xaml richeditbox

似乎UWP的richeditbox中缺少GetCharIndexFromPosition。我想在RichEditBox中悬停某个范围时显示工具提示。这可能与UWP有关吗?

1 个答案:

答案 0 :(得分:0)

在UWP中,我们可以使用GetRangeFromPoint(Point, PointOptions)方法作为GetCharIndexFromPosition的等效方法。此方法检索屏幕上特定点处或最近的退化(空)文本范围。它返回ITextRange对象,ITextRange的{​​{3}}属性类似于GetCharIndexFromPosition方法返回的字符索引。

以下是一个简单的示例:

<强> XAML:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <RichEditBox x:Name="editor" />
</Grid>

<强>代码隐藏:

public MainPage()
{
    this.InitializeComponent();
    editor.Document.SetText(Windows.UI.Text.TextSetOptions.None, @"This is a text for testing.");
    editor.AddHandler(PointerMovedEvent, new PointerEventHandler(editor_PointerMoved), true);
}

private void editor_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    var position = e.GetCurrentPoint(editor).Position;

    var range = editor.Document.GetRangeFromPoint(position, Windows.UI.Text.PointOptions.ClientCoordinates);

    System.Diagnostics.Debug.WriteLine(range.StartPosition);
}