简体   繁体   English

派生 FixedDocument 的序列化

[英]Serialization of derived FixedDocument

Since you can only add pages to a FixedDocument, I wrote a derived class:由于只能向 FixedDocument 添加页面,所以我写了一个派生类:

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

to replace FixedDocument, which works fine, until I try to print the document and receive the following error:替换 FixedDocument,它工作正常,直到我尝试打印文档并收到以下错误:

An unhandled exception of type 'System.Windows.Xps.XpsSerializationException' occurred in ReachFramework.dll ReachFramework.dll 中发生类型为“System.Windows.Xps.XpsSerializationException”的未处理异常

Additional information: Serialization of this type of object is not supported.附加信息:不支持此类对象的序列化。

I haven't worked with serialization that much in the past and have read up on it but still can't solve the issues.过去我没有做过那么多的序列化工作,并且已经阅读了它,但仍然无法解决问题。 I have also tried the我也试过

[Serializable]

attribute, but it doesn't make any difference.属性,但它没有任何区别。

Can anybody guide me in the correct direction or have any ideas what to do?任何人都可以指导我朝着正确的方向前进或有任何想法该怎么做?

If you look at decompiled source code of the method which checks if certain type is supported, you will see roughly the following:如果您查看检查是否支持某种类型的方法的反编译源代码,您将大致看到以下内容:

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;
}

Here you see that this type should either inherit DocumentPaginator, Visual, or be exactly of type FixedDocument, FixedDocumentSequence, FixedPage.在这里您可以看到,该类型应该继承 DocumentPaginator、Visual,或者正好是 FixedDocument、FixedDocumentSequence、FixedPage 类型。 So, types inherited from FixedDocument will not work, whatever serializable attributes you will use, so you have to find a different approach.因此,从 FixedDocument 继承的类型将不起作用,无论您将使用什么可序列化的属性,因此您必须找到不同的方法。 I think that is a bug in XpsSerializationManager, but maybe there is some deep reason.我认为这是 XpsSerializationManager 中的一个错误,但也许有一些深层原因。

I decided to try the OP's approach and see if I can get it to work.我决定尝试 OP 的方法,看看我是否可以让它工作。

According to the snippet posted by Evk, although the IsSerializedObjectTypeSupported() function will not accept our own custom derivative of FixedDocument , it will accept a DocumentPaginator , and one of the overloads of XpsDocumentWriter.Write() accepts a paginator, so that should work, right?根据 Evk 发布的片段,虽然IsSerializedObjectTypeSupported()函数不接受我们自己的FixedDocument的自定义派生FixedDocument ,但它会接受DocumentPaginator ,并且XpsDocumentWriter.Write()的重载之一接受分页器,所以应该可以工作,对?

Well, it turns out that if you do XpsDocumentWriter.Write( myFixedDocument.DocumentPaginator ) (where myFixedDocument is a custom derivative of FixedDocument ) then something throws a NullReferenceException somewhere deep in library code.那么,事实证明,如果你做XpsDocumentWriter.Write( myFixedDocument.DocumentPaginator )其中myFixedDocument是一个定制的衍生FixedDocument ),那么东西抛出一个NullReferenceException在库中的代码深的地方。 So, no luck there.所以,那里没有运气。

However, according to the same snippet, a FixedPage is also a supported type, and the XpsDocumentWriter.Write() method has another overload which accepts individual instances of FixedPage .但是,根据同一代码段, FixedPage也是受支持的类型,并且XpsDocumentWriter.Write()方法有另一个重载,它接受FixedPage单个实例。

So, the following code worked for me:因此,以下代码对我有用:

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

(Where Select() comes from using System.Linq ) (其中Select()来自using System.Linq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM