在 WPF 中实现解析/可编辑 Richtextbox 的最佳方法

发布于 2024-08-24 19:04:56 字数 294 浏览 3 评论 0原文

我正在尝试实现(最初作为原型)一个富文本框控件,该控件可以实时解析以对其应用某些格式选项。这是在 WPF 中完成的,因此我认为最好的方法是扩展现有的富文本框控件。

我遇到了一个问题,它没有得到很好的记录,并且示例非常慢(我发现的示例在每次击键时都会解析整个文档)。

我目前决定采用的方法是创建一个自定义内联元素,它可以保存格式标记和内容。因此,我只需解析当前段落和该段落中的运行即可格式化标签。

有更好的方法来实现这一目标吗?请注意,这不适用于基于代码/语法的文档(因此 AvalonEdit 不合适)。

干杯

I'm trying to implement (as a prototype initially), a richtextbox control which can be parsed in real time to apply certain formatting options to it. This is being done in WPF so I thought the best way to go about it would be to extend the existing rich text box control.

I've run into the issue where it isn't documented well and the examples are quite slow (the ones I found have parse the whole document on every keystroke).

The way I've currently decided to go about it is to create a custom Inline element which can hold the formatting tags, and the contents. Hence I only have to parse the current paragraph and the runs in that paragraph for formatting tags.

Is there a better way to achieve this? Note this isn't for code/syntax based documents (so AvalonEdit isn't appropriate).

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-08-31 19:04:56

如果您可以面向 NET Framework 3.5 及更高版本,则无需在每次更改时扫描文档:只需订阅 TextChanged 事件并使用 TextChangedEventArgs.Changes 属性即可获取更改列表。

每当您收到 TextChanged 事件时,请迭代 Changes 集合并根据 Offset、AddedLength 和 RemovedLength 构造 TextRange。然后根据需要扩展此 TextRange 以重新计算格式,然后作为单独的步骤进行格式计算和更新(在 Dispatcher.BeginInvoke 回调中),这样您就不会最终出现递归 T​​extChanged 事件。

richTextBox.TextChanged += (obj, e)
{
  var document = richTextBox.Document;
  var length = document.ContentStart.GetOffsetToPosition(document.ContentEnd);
  int totalAdd = 0;
  int totalRemove = 0;
  foreach(var change in e.Changes)
  {
    var expandBy = Math.Max(totalAdd,totalRemove);

    var startIndex = change.Offset - expandBy;
    var endIndex = changed.Offset + expandBy + Math.Max(totalAdd, totalRemove);

    startIndex = Math.Max(startIndex, 0);
    endIndex = Math.Min(endIndex, length);

    var startPointer = document.ContentStart.GetPositionAtOffset(startIndex);
    var endPointer = startPointer.GetPositionAtOffset(endIndex - startIndex);

    var range = new TextRange(startPointer, endPointer);
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
    {
      DoParsingAndFormatting(ExpandRangeToUnitOfParsing(range));
    });
    totalAdd += change.AddedLength;
    totalRemove += change.RemovedLength;
  }
};

如果要查找更改开始或结束的段落,可以使用 range.Start.Paragraphrange.End.Paragraph

此外,在许多情况下,将文档中所有文本的副本与 FlowDocument 本身分开存储会很有帮助。然后,当您对该文档应用更改时,您可以随时更新格式,而无需重新阅读该文档。请注意,文本不应存储在单个大数组中,而应切成小块(可能大约 1000 个字符)并通过按索引组织这些块的树进行访问。原因是在一个巨大数组的开头插入一个字符是非常昂贵的。

If you can target NET Framework 3.5 and above, you don't need to scan the document on every change: Just subscribe to the TextChanged event and use the TextChangedEventArgs.Changes property to get a list of changes.

Whenever you receive a TextChanged event, iterate through the Changes collection and construct a TextRange from the Offset, AddedLength, and RemovedLength. Then expand this TextRange as appropriate for recalculating formatting, then do the formatting calculation and update as a separate step (in a Dispatcher.BeginInvoke callback) so you don't end up having recursive TextChanged events.

richTextBox.TextChanged += (obj, e)
{
  var document = richTextBox.Document;
  var length = document.ContentStart.GetOffsetToPosition(document.ContentEnd);
  int totalAdd = 0;
  int totalRemove = 0;
  foreach(var change in e.Changes)
  {
    var expandBy = Math.Max(totalAdd,totalRemove);

    var startIndex = change.Offset - expandBy;
    var endIndex = changed.Offset + expandBy + Math.Max(totalAdd, totalRemove);

    startIndex = Math.Max(startIndex, 0);
    endIndex = Math.Min(endIndex, length);

    var startPointer = document.ContentStart.GetPositionAtOffset(startIndex);
    var endPointer = startPointer.GetPositionAtOffset(endIndex - startIndex);

    var range = new TextRange(startPointer, endPointer);
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
    {
      DoParsingAndFormatting(ExpandRangeToUnitOfParsing(range));
    });
    totalAdd += change.AddedLength;
    totalRemove += change.RemovedLength;
  }
};

If you want to find the paragraph where a change begins or ends, you can use range.Start.Paragraph and range.End.Paragraph.

Also, for many situations it will be helpful to store a copy of all the text in the document separately from the FlowDocument itself. Then as you apply changes to that document you can update the formatting as you go without having to reread the document. Note that the text should not be stored in a single large array, but rather snipped into small pieces (perhaps around 1000 characters) and accessed through a tree that organizes the pieces by index. The reason is that inserting a character at the beginning of a huge array is very expensive.

半葬歌 2024-08-31 19:04:56

请在 CodeProject 上查看此内容。您可能会对以下文章感兴趣,该文章涉及 RichTextBox 控件,该控件由子类化...看看这个 - 一个 扩展 RichTextBox 控件,它具有更多功能特征

Have a look here on this on CodeProject. Here's an article that might interest you in relation to RichTextBox control that is extended by subclassing...Look at this one - an Extended RichTextBox control that has more features

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文