似乎UWP的richeditbox中缺少GetCharIndexFromPosition。我想在RichEditBox中悬停某个范围时显示工具提示。这可能与UWP有关吗?
答案 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);
}