繁体   English   中英

在WPF中将PageContent / FixedPage添加到FixedDocument的正确方法是什么?

[英]What's the correct way to add a PageContent/ FixedPage to a FixedDocument in WPF?

在WPF中,为了在代码FixedDocument FixedPage添加到FixedDocument ,需要:

var page = new FixedPage();
var pageContent = new PageContent();

((IAddChild)pageContent).AddChild(page);

但这似乎是唯一的方法:

  • MSDN文档明确表示不应该这样做('此API支持.NET Framework基础结构,不能直接在您的代码中使用.'- PageContent.IAddChild.AddChild方法 )。

  • 为了将内容添加到PageContent ,必须转换为显式接口实现是很难看的。

  • 执行PageContent的基本操作并不简单。

文档实际上没有解释如何执行此操作,我找不到有关如何执行此操作的任何其他信息。 还有另外一种方法吗? 一个'正确'的方式?

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

暂无
暂无

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

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