简体   繁体   English

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

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

In WPF, in order to add a FixedPage to a FixedDocument in code one needs to: 在WPF中,为了在代码FixedDocument FixedPage添加到FixedDocument ,需要:

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

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

This appears to be the only way, however: 但这似乎是唯一的方法:

  • The MSDN documentation explicitly says one shouldn't do this ('This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.'- PageContent.IAddChild.AddChild Method ). MSDN文档明确表示不应该这样做('此API支持.NET Framework基础结构,不能直接在您的代码中使用.'- PageContent.IAddChild.AddChild方法 )。

  • It's ugly to have to cast to an explicit interface implementation in order to add the content to PageContent . 为了将内容添加到PageContent ,必须转换为显式接口实现是很难看的。

  • It's not simple to perform the basic operation of PageContent . 执行PageContent的基本操作并不简单。

The documentation doesn't actually explain how to do this and I couldn't find any other information on how to do it. 文档实际上没有解释如何执行此操作,我找不到有关如何执行此操作的任何其他信息。 Is there another way? 还有另外一种方法吗? A 'correct' way? 一个'正确'的方式?

According to MSDN documentation, you simply add a FixedPage object to the PageContent.Child property and then add that to the FixedDocument by calling the FixedDocument.Pages.Add method. 根据MSDN文档,只需添加一个FixedPage对象到PageContent.Child属性,然后添加到FixedDocument通过调用FixedDocument.Pages.Add方法。

For instance: 例如:

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