我尝试按照基于段落的交互方式做一些事情,就像许多电子书阅读器一样(他们让你点击一个段落并显示一个带有一些选项的小菜单等)。
我坚持的是确定用户点击/点击的段落。事件无法直接分配给Block
类型的对象,因此我尝试通过将DoubleTapped
事件处理程序附加到父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;
}
但这种做法显然不会奏效。
我在这里找不到什么东西?
谢谢!
答案 0 :(得分: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
的{{1}}属性设置为RichTextBlock
,则无需从代码中手动注册false
事件处理程序在XAML注册后,它只会起作用:
DoubleTapped