0

我正在尝试按照基于段落的交互来做一些事情,就像许多电子书阅读器所做的那样(它们让您单击一个段落并显示一个带有一些选项的小菜单等)。

我坚持的事情是确定用户点击/点击的段落。事件不能直接分配给Block类型对象,因此我尝试通过将DoubleTapped事件处理程序附加到 parent 来解决它RichTetBlock。我在想可能以某种方式根据位置确定段落,但我不完全确定如何在实践中进行。

到目前为止我得到了什么:

this.chapterText.AddHandler(DoubleTappedEvent, new DoubleTappedEventHandler(chapterText_DoubleTapped), true);
...
private void chapterText_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    var rtb = sender as RichTextBlock;
    var y = e.GetPosition(rtb).Y;
    var paragraph = rtb.Blocks.FirstOrDefault(o => y >= o.) as Paragraph;
}

但这种方法显然行不通。

我在这里缺少什么吗?

谢谢!

4

1 回答 1

0

到目前为止我所拥有的...

您快到了:

    private void RichTextBlock_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
    {
        RichTextBlock richTextBlock = sender as RichTextBlock;
        TextPointer textPointer = richTextBlock.GetPositionFromPoint(e.GetPosition(richTextBlock));
        Paragraph paragraph = richTextBlock.Blocks.Where(b => b.ContentStart.Offset <= textPointer.Offset && b.ContentEnd.Offset >= textPointer.Offset).FirstOrDefault() as Paragraph;
    }

或者

    private void RichTextBlock_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
    {
        RichTextBlock richTextBlock = sender as RichTextBlock;
        TextPointer textPointer = richTextBlock.GetPositionFromPoint(e.GetPosition(richTextBlock));
        Run run = textPointer.Parent as Run;
        Paragraph paragraph = run.ElementStart.Parent as Paragraph;
    }

顺便说一句,如果您将IsTextSelectionEnabled属性设置为RichTextBlockto false,则不必DoubleTapped从后面的代码中手动注册事件处理程序,它只需在 XAML 中注册后即可工作:

<RichTextBlock IsTextSelectionEnabled="False" DoubleTapped="RichTextBlock_DoubleTapped">
于 2017-12-01T02:02:17.280 回答