【发布时间】:2017-04-04 15:26:03
【问题描述】:
UWP 的 Richeditbox 中似乎缺少 GetCharIndexFromPosition。当某个范围悬停在 RichEditBox 中时,我想显示一个工具提示。 UWP 可以做到这一点吗?
【问题讨论】:
标签: c# xaml uwp uwp-xaml richeditbox
UWP 的 Richeditbox 中似乎缺少 GetCharIndexFromPosition。当某个范围悬停在 RichEditBox 中时,我想显示一个工具提示。 UWP 可以做到这一点吗?
【问题讨论】:
标签: c# xaml uwp uwp-xaml richeditbox
在 UWP 中,我们可以使用 GetRangeFromPoint(Point, PointOptions) 方法作为 GetCharIndexFromPosition 的等价物。此方法检索屏幕上特定点处或最近的退化(空)文本范围。它返回一个ITextRange 对象,ITextRange 的StartPosition 属性类似于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);
}
【讨论】: