UWP RichEditBox эквивалент GetCharIndexFromPosition
Похоже, что GetCharIndexFromPosition отсутствует в richeditbox UWP. Я хочу отобразить всплывающую подсказку, когда определенный диапазон находится в RichEditBox. Это возможно с UWP?
1 ответ
Решение
В UWP мы можем использовать метод GetRangeFromPoint(Point, PointOptions) как эквивалент GetCharIndexFromPosition
, Этот метод извлекает вырожденный (пустой) текстовый диапазон в определенной точке экрана или рядом с ней. Возвращает объект ITextRange и свойство StartPosition ITextRange
похож на индекс символа, возвращаемый 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);
}