为什么打印XpsDocument时我的数据绑定失败? [英] Why am I losing my databinding when printing to an XpsDocument?

查看:170
本文介绍了为什么打印XpsDocument时我的数据绑定失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新!



绑定工作。问题是XpsDocumentWriter没有正确地写入FixedDocumentSequence的第一个文档的第一页。这似乎是很多人在做这样的事情(即全球五个开发商)遇到的问题。解决方案有点奇怪。我把它作为一个答案。






好吧,它比这个问题显得更微妙。 b
$ b

我有一系列的固定页面,每个都有其DataContext单独设置。每个FixedPage也有一个或多个绑定到上下文的控件。



如果我将这些FixedPages添加到一个FixedDocument,并将这个固定文档写入XpsDocument,我的绑定被取消引用(可以这么说),正确的值在XpsDocument中显示。



如果我将这些FixedPages添加到单个FixedDocuments(每个FP被添加到一个新的FD),那么这些FixedDocuments被添加到一个FixedDocumentSequence中,然后这个序列被写入到XpsDocument中,我的绑定不被引用,我的FixedPages显示为空。



调试告诉我,我没有失去我的绑定或绑定上下文,所以这不是这个失败的原因。



下面是一些示例代码来说明发生了什么:

  //这个工程
FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
//将我的数据绑定固定页面添加到新的固定文档
var fd = new FixedDocument();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
//创建一个xps文档并写入我的固定文档
var p = Package.Open(c:\\\output.xps,FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fd);
p.Flush();
p.Close();

//这不工作
FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
//将我的数据绑定固定页面添加到新的固定文档
var fd = new FixedDocument();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
//创建一个固定的文档序列,并将固定文档添加到它
FixedDocumentSequence fds = CreateFixedDocumentSequence();
var dr = new DocumentReference();
dr.BeginInit();
dr.SetDocument(fd);
dr.EndInit();
(fds as IAddChild).AddChild(dr);
//创建一个xps文档并将固定的文档序列写入
var p = Package.Open(c:\\\output.xps,FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fds);
p.Flush();
p.Close();

您可以看到,两者之间的唯一区别是我将固定文档添加到固定文件序列,然后写入。



显然,无论什么魔法发生,导致数据绑定被评估,并且绑定值插入不会发生,当我的固定文档不被写入Xps文档。我需要能够编写多个固定文档,而Write方法只能调用一次,因此需要将FixedDocuments添加到一个FixedDocumentSequence中,然后我再写入。但是我也还需要我的数据绑定工作!



在这种情况下的任何帮助都不胜感激。我知道它不是框架中最常见的部分;我只是希望有人在这里有一些操作经验(我正在看你,潜伏MS员工)。

解决方案

p>这个bug的原因是在编写前没有更新FixedPage的布局。这会导致在FixedDocumentSequence中的第一个FixedDocument中的第一个FixedPage被错误地写入。这影响了在结果文档中没有其他页面,这使得这个错误/边缘情况更难打破。



以下WORKS(重写版本的非工作示例):

  FixedPage fp = CreateFixedPageWithBinding(); 
fp.DataContext = CreateDataContext();
var fd = new FixDocument();

/ * PAY ATTENTION HERE * /
//设置固定文档上的页面大小
fd.DocumentPaginator.PageSize =
new System.Windows.Size( )
{
宽度= DotsPerInch * PageWidth,
Height = DotsPerInch * PageHeight
};
//更新我们的FixedPage的布局
var size = fd.DocumentPaginator.PageSize;
page.Measure(size);
page.Arrange(new Rect(new Point(),size));
page.UpdateLayout();
/ *停止付款注意* /

var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
//创建一个固定的文档序列,并将固定文档添加到它
FixedDocumentSequence fds = CreateFixedDocumentSequence();
var dr = new documentReference();
dr.BeginInit();
dr.SetDocument(fd);
dr.EndInit();
(fds as IAddChild).AddChild(dr);
//创建一个xps文档并将固定的文档序列写入
var p = Package.Open(c:\\\output.xps,FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fds);
p.Flush();
p.Close();


Update!

Binding works. The issue is that the XpsDocumentWriter doesn't properly write the first page of the first document of a FixedDocumentSequence. This seems to be an issue encountered by lots of people doing this sort of thing (i.e., five developers worldwide). The solution is slightly odd. I include it as an answer.


Okay, its a bit more subtle than the question suggests.

I've got a series of FixedPages, each has its DataContext set individually. Each FixedPage also has one or more controls that are bound to the context.

If I add these FixedPages to a single FixedDocument and write this single FixedDocument to an XpsDocument, my binds are de-referenced (so to speak) and the correct values are presented in the XpsDocument.

If I add these FixedPages to individual FixedDocuments (each FP gets added to a new FD), then these FixedDocuments are added to a FixedDocumentSequence, and this sequence is then written to the XpsDocument, my binds are NOT de-referenced and my FixedPages appear blank.

Debugging tells me that I'm not losing my bindings or my binding context, so that's not the cause of this failure.

Here's some sample code to illustrate what's going on:

// This works
FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
// Add my databound fixed page to a new fixed document
var fd = new FixedDocument();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
// Create an xps document and write my fixed document to it
var p = Package.Open("c:\\output.xps", FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fd);
p.Flush();
p.Close();

// This does NOT work
FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
// Add my databound fixed page to a new fixed document
var fd = new FixedDocument();
var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
// Create a fixed document sequence and add the fixed document to it
FixedDocumentSequence fds = CreateFixedDocumentSequence();
var dr = new DocumentReference();
dr.BeginInit();
dr.SetDocument(fd);
dr.EndInit();
(fds as IAddChild).AddChild(dr);
// Create an xps document and write the fixed document sequence to it
var p = Package.Open("c:\\output.xps", FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fds);
p.Flush();
p.Close();

You can see that the only difference between the two is that I'm adding the fixed document to a fixed document sequence, which then gets written.

Obviously, whatever magic happens that causes the databinding to be evaluated and the bound values be inserted isn't happening when my fixed documents aren't being Written to the Xps Document. I need to be able to write more than one fixed document, and the Write method can only be called once, thus requiring I add the FixedDocuments to a FixedDocumentSequence which I then write. But I also need my damn databinding to work as well!

Any help in this situation would be appreciated. I know its not exactly the most common part of the framework; I'm just hoping that someone here has some operational experience with this (I'm looking at you, lurking MS employee).

解决方案

The cause of this bug is that the FixedPage's layout isn't being updated prior to writing. This causes the first FixedPage in the first FixedDocument in the FixedDocumentSequence to be written incorrectly. This affects NO OTHER PAGES IN THE RESULTING DOCUMENT, which made this bug/edge case harder to nail down.

The following WORKS (rewritten version of the non-working example):

FixedPage fp = CreateFixedPageWithBinding();
fp.DataContext = CreateDataContext();
var fd = new FixedDocument();

/* PAY ATTENTION HERE */
// set the page size on our fixed document 
fd.DocumentPaginator.PageSize =
   new System.Windows.Size()
   {
       Width = DotsPerInch * PageWidth,
       Height = DotsPerInch * PageHeight
   };
// Update the layout of our FixedPage
var size = fd.DocumentPaginator.PageSize;
page.Measure(size);
page.Arrange(new Rect(new Point(), size));
page.UpdateLayout();    
/* STOP PAYING ATTENTION HERE */

var pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fd.Pages.Add(pageContent);
// Create a fixed document sequence and add the fixed document to it
FixedDocumentSequence fds = CreateFixedDocumentSequence();
var dr = new DocumentReference();
dr.BeginInit();
dr.SetDocument(fd);
dr.EndInit();
(fds as IAddChild).AddChild(dr);
// Create an xps document and write the fixed document sequence to it
var p = Package.Open("c:\\output.xps", FileMode.CreateNew);
var doc = new XpsDocument(p);
var writer = XpsDocument.CreateXpsDocumentWriter(doc);
wri2.Write(fds);
p.Flush();
p.Close();

这篇关于为什么打印XpsDocument时我的数据绑定失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆