我希望能够加载大的 .rtf 文件,并滚动到其中特定的段落。
因此,我会执行以下操作:
private FlowDocument GenerateDocument(string path)
{
var doc = new FlowDocument();
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read))
{
var content = new TextRange(doc.ContentStart, doc.ContentEnd);
content.Load(fs, DataFormats.Rtf);
}
return doc;
}
然后,我查找了已经加书签的段落,想要滚动到该处。类似于这样:
private void ScrollToBookmark(string bookmark)
{
Paragraph p = LookUp(bookmark);
if (p != null) p.BringIntoView();
}
LookUp是一种查找所需段落的函数,它能够很好地工作,并返回先前加载的文档中的段落。
但是...什么都没有发生。有什么提示吗?
我使用FlowDocumentReader来显示我的Flowdocument。在ScrollMode和PageMode中,BringIntoView都无法正常工作。
直接通过逻辑树访问FlowDocumentReader的scrollviewer并对其进行操作不是一种选择。我根本不知道该滚动到哪里,如果可能的话,我更喜欢不使用任何“hack”。