使用XamlReader和XamlWriter时,将一个FlowDocument中的内容插入另一个
本文关键字:FlowDocument 一个 另一个 插入 XamlWriter XamlReader 使用 | 更新日期: 2023-09-27 18:20:40
我将FlowDocument与BlockUIContainer和InlineUIContainer元素一起使用,这些元素包含(或作为基类)一些自定义块-SVG、数学公式等。因此,使用Selection.Load(流,DataFormats.XamlPackage)将无法工作,因为序列化将删除*UIContainers的内容,除非Child属性是Microsoft参考源中提供的图像
private static void WriteStartXamlElement(...)
{
...
if ((inlineUIContainer == null || !(inlineUIContainer.Child is Image)) &&
(blockUIContainer == null || !(blockUIContainer.Child is Image)))
{
...
elementTypeStandardized = TextSchema.GetStandardElementType(elementType, /*reduceElement:*/true);
}
...
}
在这种情况下,唯一可以使用的选项是使用工作正常的XamlWriter.Save和XamlReader.Load,序列化和反序列化FlowDocument的所有必需属性和对象,但复制+粘贴必须手动实现,因为复制+粘贴的默认实现使用Selection.Load/Save.
复制/粘贴至关重要,因为它还用于处理RichTextBox控件中或控件之间的元素拖动,这是在没有自定义拖动代码的情况下操作对象的唯一方法。
这就是为什么我希望使用FlowDocument序列化实现复制/粘贴,但不幸的是,它存在一些问题:
- 在当前的解决方案中,需要对整个FlowDocument对象进行序列化/反序列化。就性能而言,这应该不是问题,但我需要存储需要从中粘贴的选择范围的信息(CustomRichTextBoxTag类)
显然,对象不能从一个文档中删除并添加到另一个文档(我最近发现了一个死胡同):"InlineCollection"元素不能插入树中,因为它已经是树的子元素。
[TextElementCollection.cs] public void InsertAfter(TextElementType previousSibling, TextElementType newItem) { ... if (previousSibling.Parent != this.Parent) throw new InvalidOperationException(System.Windows.SR.Get("TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection", new object[1] { (object) previousSibling.GetType().Name })); ... }
我考虑设置FrameworkContentElement_父级在所有需要移动到另一个文档的元素中使用反射,但这是最后的解决方案:
理论上,我只能复制所需的对象:(可选)在选择开始时用文本部分运行,和之间的所有段落和内联,以及(可能)在结束时部分运行,将它们封装在自定义类中,并使用XamlReader/XamlWriter进行序列化/反序列化。
- 另一个我没有想过的解决方案
以下是自定义RichTextBox控件的实现,带有部分工作的自定义复制/粘贴代码:
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
namespace FlowMathTest
{
public class CustomRichTextBoxTag: DependencyObject
{
public static readonly DependencyProperty SelectionStartProperty = DependencyProperty.Register(
"SelectionStart",
typeof(int),
typeof(CustomRichTextBoxTag));
public int SelectionStart
{
get { return (int)GetValue(SelectionStartProperty); }
set { SetValue(SelectionStartProperty, value); }
}
public static readonly DependencyProperty SelectionEndProperty = DependencyProperty.Register(
"SelectionEnd",
typeof(int),
typeof(CustomRichTextBoxTag));
public int SelectionEnd
{
get { return (int)GetValue(SelectionEndProperty); }
set { SetValue(SelectionEndProperty, value); }
}
}
public class CustomRichTextBox: RichTextBox
{
public CustomRichTextBox()
{
DataObject.AddCopyingHandler(this, OnCopy);
DataObject.AddPastingHandler(this, OnPaste);
}
protected override void OnSelectionChanged(RoutedEventArgs e)
{
base.OnSelectionChanged(e);
var tag = Document.Tag as CustomRichTextBoxTag;
if(tag == null)
{
tag = new CustomRichTextBoxTag();
Document.Tag = tag;
}
tag.SelectionStart = Document.ContentStart.GetOffsetToPosition(Selection.Start);
tag.SelectionEnd = Document.ContentStart.GetOffsetToPosition(Selection.End);
}
private void OnCopy(object sender, DataObjectCopyingEventArgs e)
{
if(e.DataObject != null)
{
e.Handled = true;
var ms = new MemoryStream();
XamlWriter.Save(Document, ms);
e.DataObject.SetData(DataFormats.Xaml, ms);
}
}
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
var xamlData = e.DataObject.GetData(DataFormats.Xaml) as MemoryStream;
if(xamlData != null)
{
xamlData.Position = 0;
var fd = XamlReader.Load(xamlData) as FlowDocument;
if(fd != null)
{
var tag = fd.Tag as CustomRichTextBoxTag;
if(tag != null)
{
InsertAt(Document, Selection.Start, Selection.End, fd, fd.ContentStart.GetPositionAtOffset(tag.SelectionStart), fd.ContentStart.GetPositionAtOffset(tag.SelectionEnd));
e.Handled = true;
}
}
}
}
public static void InsertAt(FlowDocument destDocument, TextPointer destStart, TextPointer destEnd, FlowDocument sourceDocument, TextPointer sourceStart, TextPointer sourceEnd)
{
var destRange = new TextRange(destStart, destEnd);
destRange.Text = string.Empty;
// insert partial text of the first run in the selection
if(sourceStart.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
var sourceRange = new TextRange(sourceStart, sourceStart.GetNextContextPosition(LogicalDirection.Forward));
destStart.InsertTextInRun(sourceRange.Text);
sourceStart = sourceStart.GetNextContextPosition(LogicalDirection.Forward);
destStart = destStart.GetNextContextPosition(LogicalDirection.Forward);
}
var field = typeof(FrameworkContentElement).GetField("_parent", BindingFlags.NonPublic | BindingFlags.Instance);
while(sourceStart != null && sourceStart.CompareTo(sourceEnd) <= 0 && sourceStart.Paragraph != null)
{
var sourceInline = sourceStart.Parent as Inline;
if(sourceInline != null)
{
sourceStart.Paragraph.Inlines.Remove(sourceInline);
if(destStart.Parent is Inline)
{
field.SetValue(sourceInline, null);
destStart.Paragraph.Inlines.InsertAfter(destStart.Parent as Inline, sourceInline);
}
else
{
var p = new Paragraph();
destDocument.Blocks.InsertAfter(destStart.Paragraph, p);
p.Inlines.Add(sourceInline);
}
sourceStart = sourceStart.GetNextContextPosition(LogicalDirection.Forward);
}
else
{
var sourceBlock = sourceStart.Parent as Block;
field.SetValue(sourceBlock, null);
destDocument.Blocks.InsertAfter(destStart.Paragraph, sourceBlock);
sourceStart = sourceStart.GetNextContextPosition(LogicalDirection.Forward);
}
}
}
}
}
问题是,是否存在使用XamlReader和XamlWriter为FlowDocument自定义复制+粘贴代码的现有解决方案?如何修复上面的代码,使其不会抱怨不同的FlowDocument对象或绕过此限制?
编辑:作为一个实验,我实现了2),这样对象就可以从一个FlowDocument移动到另一个。上面的代码已更新-所有对"字段"变量的引用。
赏金期似乎即将到期,我在如何实现上述问题方面取得了突破,所以我将在这里分享。
首先,TextRange.Save有一个"preserveTextElements"参数,可用于序列化InlineUIContainer和BlockUIContainer元素。此外,这两个控件都不是密封的,因此可以用作自定义TextElement实现的基类。
考虑到以上内容:
-
我创建了一个继承自InlineUIContainer的InlineMedia元素,该元素使用XamlReader和XamlWriter将其子项"手动"序列化为"ChildSource"依赖属性,并从默认序列化程序中隐藏原始的"Child"
-
我将CustomRichTextBox的上述实现更改为使用范围复制选择。保存(ms,DataFormats.Xaml,true)。
正如您所注意到的,不需要特殊的粘贴处理,因为在剪贴板中交换原始Xaml后,Xaml被很好地反序列化了,这意味着拖动可以从所有CustomRichtextBox控件中复制,粘贴甚至可以工作到普通的RichTextBox中。
唯一的限制是,对于所有InlineMedia控件,在序列化整个文档之前,需要通过序列化它的Child来更新ChildSource属性,我找不到自动更新的方法(在保存元素之前挂入TextRange.Save)。
我可以接受,但如果没有这个问题,一个更好的解决方案仍然会得到奖励!
InlineMedia元素代码:
public class InlineMedia: InlineUIContainer
{
public InlineMedia()
{
}
public InlineMedia(UIElement childUIElement) : base(childUIElement)
{
UpdateChildSource();
}
public InlineMedia(UIElement childUIElement, TextPointer insertPosition)
: base(childUIElement, insertPosition)
{
UpdateChildSource();
}
public static readonly DependencyProperty ChildSourceProperty = DependencyProperty.Register
(
"ChildSource",
typeof(string),
typeof(InlineMedia),
new FrameworkPropertyMetadata(null, OnChildSourceChanged));
public string ChildSource
{
get
{
return (string)GetValue(ChildSourceProperty);
}
set
{
SetValue(ChildSourceProperty, value);
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new UIElement Child
{
get
{
return base.Child;
}
set
{
base.Child = value;
UpdateChildSource();
}
}
public void UpdateChildSource()
{
IsInternalChildSourceChange = true;
try
{
ChildSource = Save();
}
finally
{
IsInternalChildSourceChange = false;
}
}
public string Save()
{
if(Child == null)
{
return null;
}
using(var stream = new MemoryStream())
{
XamlWriter.Save(Child, stream);
stream.Position = 0;
using(var reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
public void Load(string sourceData)
{
if(string.IsNullOrEmpty(sourceData))
{
base.Child = null;
}
else
{
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(sourceData)))
{
var child = XamlReader.Load(stream);
base.Child = (UIElement)child;
}
}
}
private static void OnChildSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var img = (InlineMedia) sender;
if(img != null && !img.IsInternalChildSourceChange)
{
img.Load((string)e.NewValue);
}
}
protected bool IsInternalChildSourceChange { get; private set; }
}
CustomRichTextBox控制代码:
public class CustomRichTextBox: RichTextBox
{
public CustomRichTextBox()
{
DataObject.AddCopyingHandler(this, OnCopy);
}
private void OnCopy(object sender, DataObjectCopyingEventArgs e)
{
if(e.DataObject != null)
{
UpdateDocument();
var range = new TextRange(Selection.Start, Selection.End);
using(var ms = new MemoryStream())
{
range.Save(ms, DataFormats.Xaml, true);
ms.Position = 0;
using(var reader = new StreamReader(ms, Encoding.UTF8))
{
var xaml = reader.ReadToEnd();
e.DataObject.SetData(DataFormats.Xaml, xaml);
}
}
e.Handled = true;
}
}
public void UpdateDocument()
{
ObjectHelper.ExecuteRecursive<InlineMedia>(Document, i => i.UpdateChildSource(), FlowDocumentVisitors);
}
private static readonly Func<object, object>[] FlowDocumentVisitors =
{
x => (x is FlowDocument) ? ((FlowDocument) x).Blocks : null,
x => (x is Section) ? ((Section) x).Blocks : null,
x => (x is BlockUIContainer) ? ((BlockUIContainer) x).Child : null,
x => (x is InlineUIContainer) ? ((InlineUIContainer) x).Child : null,
x => (x is Span) ? ((Span) x).Inlines : null,
x => (x is Paragraph) ? ((Paragraph) x).Inlines : null,
x => (x is Table) ? ((Table) x).RowGroups : null,
x => (x is Table) ? ((Table) x).Columns : null,
x => (x is Table) ? ((Table) x).RowGroups.SelectMany(rg => rg.Rows) : null,
x => (x is Table) ? ((Table) x).RowGroups.SelectMany(rg => rg.Rows).SelectMany(r => r.Cells) : null,
x => (x is TableCell) ? ((TableCell) x).Blocks : null,
x => (x is TableCell) ? ((TableCell) x).BorderBrush : null,
x => (x is List) ? ((List) x).ListItems : null,
x => (x is ListItem) ? ((ListItem) x).Blocks : null
};
}
最后是ObjectHelper类-访问者助手:
public static class ObjectHelper
{
public static void ExecuteRecursive(object item, Action<object> execute, params Func<object, object>[] childSelectors)
{
ExecuteRecursive<object, object>(item, null, (c, i) => execute(i), childSelectors);
}
public static void ExecuteRecursive<TObject>(object item, Action<TObject> execute, params Func<object, object>[] childSelectors)
{
ExecuteRecursive<object, TObject>(item, null, (c, i) => execute(i), childSelectors);
}
public static void ExecuteRecursive<TContext, TObject>(object item, TContext context, Action<TContext, TObject> execute, params Func<object, object>[] childSelectors)
{
ExecuteRecursive(item, context, (c, i) =>
{
if(i is TObject)
{
execute(c, (TObject)i);
}
}, childSelectors);
}
public static void ExecuteRecursive<TContext>(object item, TContext context, Action<TContext, object> execute, params Func<object, object>[] childSelectors)
{
execute(context, item);
if(item is IEnumerable)
{
foreach(var subItem in item as IEnumerable)
{
ExecuteRecursive(subItem, context, execute, childSelectors);
}
}
if(childSelectors != null)
{
foreach(var subItem in childSelectors.Select(x => x(item)).Where(x => x != null))
{
ExecuteRecursive(subItem, context, execute, childSelectors);
}
}
}
}
1.在当前的解决方案中,需要对整个FlowDocument对象进行序列化/反序列化。就性能而言,这应该不是问题但我需要存储选择范围的信息从中粘贴(CustomRichTextBoxTag类)。
这听起来像是一个根据您确定的预期行为使用附加属性的机会。我将附加属性理解为向元素添加附加行为的一种方式。注册附加属性时,可以为该属性值更改时添加事件处理程序。为了利用这一点,我将此附加属性关联到DataTrigger,以更新复制/粘贴操作的选择范围值。
2.显然,对象无法从一个文档中删除并添加到另一个文档(我最近发现了一个死胡同):"InlineCollection"元素无法插入到树中,因为它已经是树的子级。
您可以通过以编程方式构建元素并以编程方式删除元素来解决此问题。在一天结束时,您主要处理ItemsControl或ContentControl。在这种情况下,您使用ItemsControl(即文档)。因此,只需以编程方式从ItemsControl(文档)中添加和删除子元素即可。