UWP RichEditBox 相当于 GetCharIndexFromPosition
UWP RichEditBox equivalent of GetCharIndexFromPosition
UWP 的 richeditbox 中似乎缺少 GetCharIndexFromPosition。当某个范围悬停在 RichEditBox 中时,我想显示一个工具提示。 UWP 可以做到这一点吗?
在UWP中,我们可以使用GetRangeFromPoint(Point, PointOptions) method as the equivalent of GetCharIndexFromPosition
. This method retrieves the degenerate (empty) text range at, or nearest to, a particular point on the screen. It returns a ITextRange object and the StartPosition 属性 of 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);
}
UWP 的 richeditbox 中似乎缺少 GetCharIndexFromPosition。当某个范围悬停在 RichEditBox 中时,我想显示一个工具提示。 UWP 可以做到这一点吗?
在UWP中,我们可以使用GetRangeFromPoint(Point, PointOptions) method as the equivalent of GetCharIndexFromPosition
. This method retrieves the degenerate (empty) text range at, or nearest to, a particular point on the screen. It returns a ITextRange object and the StartPosition 属性 of 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);
}