【问题标题】:Serialization of derived FixedDocument派生的 FixedDocument 的序列化
【发布时间】:2015-12-05 02:34:39
【问题描述】:

由于只能给FixedDocument添加页面,所以我写了一个派生类:

public class CustomFixedDocument : FixedDocument
{
    public void RemoveChild(object child)
    {
        base.RemoveLogicalChild(child);
    }
}

替换FixedDocument,它工作正常,直到我尝试打印文档并收到以下错误:

未处理的类型异常 'System.Windows.Xps.XpsSerializationException' 发生在 ReachFramework.dll

附加信息:此类对象的序列化不是 支持。

我过去没有过多地使用序列化,并且已经阅读过它,但仍然无法解决问题。我也试过了

[Serializable]

属性,但没有任何区别。

任何人都可以指导我正确的方向或有什么想法吗?

【问题讨论】:

  • 具体是怎么打印的?
  • 我添加了一个绑定到FixedDocumentDocumentViewer对象:<DocumentViewer Document="{Binding Path=DocumentView}"/>
  • DocumentViewer 然后添加打印所需的所有控件。然后我只需单击打印按钮选择一台打印机并打印文档。测试的时候我只是选择了pdf打印机,此时正好出现错误(选择pdf打印机点击打印后)

标签: c# wpf serialization xps


【解决方案1】:

如果您查看检查是否支持某种类型的方法的反编译源代码,您将大致看到以下内容:

internal bool IsSerializedObjectTypeSupported(object serializedObject)
{
  bool flag = false;
  Type type = serializedObject.GetType();
  if (this._isBatchMode)
  {
    if (typeof (Visual).IsAssignableFrom(type) && type != typeof (FixedPage))
      flag = true;
  }
  else if (type == typeof (FixedDocumentSequence) || type == typeof (FixedDocument) || (type == typeof (FixedPage) || typeof (Visual).IsAssignableFrom(type)) || typeof (DocumentPaginator).IsAssignableFrom(type))
    flag = true;
  return flag;
}

在这里您可以看到该类型应该继承 DocumentPaginator、Visual,或者完全属于 FixedDocument、FixedDocumentSequence、FixedPage 类型。因此,从 FixedDocument 继承的类型将不起作用,无论您将使用什么可序列化属性,因此您必须找到不同的方法。我认为这是 XpsSerializationManager 中的一个错误,但也许有一些深层次的原因。

【讨论】:

  • 很不幸,我认为从 FixedDocument 中删除页面的问题已解决。无论如何,回到绘图板......
【解决方案2】:

我决定试试 OP 的方法,看看能不能让它发挥作用。

根据 Evk 发布的 sn-p,虽然IsSerializedObjectTypeSupported() 函数不会接受我们自己自定义的 FixedDocument 派生类,但它会接受 DocumentPaginator,而 XpsDocumentWriter.Write() 的重载之一接受分页器,这样应该可以工作吧?

好吧,事实证明,如果您执行XpsDocumentWriter.Write( myFixedDocument.DocumentPaginator )(其中myFixedDocumentFixedDocument 的自定义派生词),那么在库代码深处的某个地方会出现NullReferenceException。所以,没有运气。

但是,根据相同的 sn-p,FixedPage 也是受支持的类型,XpsDocumentWriter.Write() 方法还有另一个重载,它接受 FixedPage 的各个实例。

所以,以下代码对我有用:

foreach( FixedPage fixedPage in 
        fixedDocument.Pages.Select( pageContent => pageContent.Child ) )
    xpsDocumentWriter.Write( fixedPage );

(其中Select() 来自using System.Linq

【讨论】:

    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2017-06-24
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多