【问题标题】:UWP RichEditBox equivalent of GetCharIndexFromPositionUWP RichEditBox 等效于 GetCharIndexFromPosition
【发布时间】:2017-04-04 15:26:03
【问题描述】:

UWP 的 Richeditbox 中似乎缺少 GetCharIndexFromPosition。当某个范围悬停在 RichEditBox 中时,我想显示一个工具提示。 UWP 可以做到这一点吗?

【问题讨论】:

    标签: c# xaml uwp uwp-xaml richeditbox


    【解决方案1】:

    在 UWP 中,我们可以使用 GetRangeFromPoint(Point, PointOptions) 方法作为 GetCharIndexFromPosition 的等价物。此方法检索屏幕上特定点处或最近的退化(空)文本范围。它返回一个ITextRange 对象,ITextRangeStartPosition 属性类似于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);
    }
    

    【讨论】:

    • 嗨杰,嘿,这对我有用!非常感谢,很抱歉延迟回复!
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2020-07-05
    • 2017-04-23
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多