RichTextBlock PointerPressedEvent - 选择文本时的指针位置

本文关键字:指针 位置 文本 PointerPressedEvent 选择 RichTextBlock | 更新日期: 2023-09-27 18:36:44

我正在开发一个Windows应用商店应用程序。在我的页面中,我有一个富文本(而不是像WPF那样的富文本框)。

我想订阅PointerPressed活动。在我的构造函数中,我有以下内容

   rtb.PointerPressed += RtbOnPointerPressed;
    rtb.PointerReleased += RtbOnPointerReleased;

我的问题是这些事件永远不会被触发。

如果我在 RichTextBlock (代码中的 TopGrid)上放一个网格,我可以捕获 pointerPressed 事件。但是,我无法再在富文本块中选择文本。

如果我尝试在容器级别捕获 pointerPressed 事件(下面的代码中的容器),如果我在选择文本时按边距而不是按边距,它会起作用。

    <Grid x:Name="Container">//at this level, pointerPressed is raised
           //only when I click outside the richtextblock
       <RichTextBlock x:Name="rtb"></RichTextBlock>//PointerPressed is never fired
       <Grid Background="Transparent" x:Name="TopGrid"></Grid>//If present,
       //the selection does not work on RichTextBlock" 
       //but I can capture the pointerPressed event
    </Grid>

这里有人知道如何在选择文本时知道指针位置吗?

RichTextBlock PointerPressedEvent - 选择文本时的指针位置

您必须处理 SelectionChanged 事件。发件人将具有 SelectionStart、SelectionEnd 和 SelectedText 属性。

如果出于某种原因想要使用"按下/已发布"事件,请将代码替换为:

RTB。AddHandler(PointerPressedEvent, new PointerEventHandler(RtbOnPointerPressed), true);

RTB。AddHandler(PointerReleaseEvent, new PointerEventHandler(RtbOnPointerRelease), true);

最后一个参数handledEventsToo必须设置为 true,以确保即使事件在其他地方处理,也会调用处理程序。

根据 https://msdn.microsoft.com/pl-pl/library/windows/apps/xaml/windows.ui.xaml.uielement.pointerpressed 你应该意识到这样一个事实,即PointerPressing和PointerRelease并不总是成对发生。