由于您只能向FixedDocument添加页面,所以我编写了一个派生类:
public class CustomFixedDocument : FixedDocument
{
public void RemoveChild(object child)
{
base.RemoveLogicalChild(child);
}
}若要替换工作正常的FixedDocument,请执行以下操作,直到我尝试打印文档并接收以下错误:
'System.Windows.Xps.XpsSerializationException‘类型的未处理异常发生在ReachFramework.dll中 附加信息:不支持此类对象的序列化。
在过去,我没有那么多地使用序列化,并且已经阅读过它,但仍然无法解决这些问题。我也试过
[Serializable]属性,但这没有任何区别。
有人能指引我正确的方向或有什么想法做什么吗?
发布于 2015-09-09 08:49:24
如果您查看该方法的反编译源代码(检查是否支持某种类型),您将大致看到以下内容:
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中的一个bug,但可能有一些深层次的原因。
发布于 2020-06-19 11:15:41
我决定试试OP的方法,看看能不能让它起作用。
根据Evk发布的片段,虽然IsSerializedObjectTypeSupported()函数将不接受我们自己的自定义派生的FixedDocument,但它将接受DocumentPaginator,XpsDocumentWriter.Write()的重载之一接受分页器,所以应该可以工作,对吗?
实际上,如果您使用XpsDocumentWriter.Write( myFixedDocument.DocumentPaginator ) (其中myFixedDocument是FixedDocument的自定义派生),那么某些东西会在库代码中抛出一个NullReferenceException。所以,那里没有运气。
但是,根据同一段代码,FixedPage也是受支持的类型,XpsDocumentWriter.Write()方法还有另一个重载,它接受FixedPage的各个实例。
因此,下面的代码适用于我:
foreach( FixedPage fixedPage in
fixedDocument.Pages.Select( pageContent => pageContent.Child ) )
xpsDocumentWriter.Write( fixedPage );( Select()来自using System.Linq)
https://stackoverflow.com/questions/32474210
复制相似问题