在WPF中,为了在代码中向FixedPage
添加FixedDocument
,我们需要:
var page = new FixedPage();
var pageContent = new PageContent();
((IAddChild)pageContent).AddChild(page);
然而,这似乎是唯一的方法:
MSDN文档明确表示不应该这样做('此API支持.NET Framework基础结构,不能直接在您的代码中使用。' - PageContent.IAddChild.AddChild Method)。
为了将内容添加到PageContent
,必须强制转换为显式接口实现。
执行PageContent
的基本操作并不简单。
文档实际上并未解释如何执行此操作,但我找不到有关如何执行此操作的任何其他信息。还有另外一种方法吗?一个'正确'的方式?
答案 0 :(得分:7)
根据MSDN文档,您只需将FixedPage
对象添加到PageContent.Child
属性,然后通过调用FixedDocument
方法将其添加到FixedDocument.Pages.Add
。
例如:
public FixedDocument RenderDocument()
{
FixedDocument fd = new FixedDocument();
PageContent pc = new PageContent();
FixedPage fp = new FixedPage();
TextBlock tb = new TextBlock();
//add some text to a TextBox object
tb.Text = "This is some test text";
//add the text box to the FixedPage
fp.Children.Add(tb);
//add the FixedPage to the PageContent
pc.Child = fp;
//add the PageContent to the FixedDocument
fd.Pages.Add(pc);
return fd;
}