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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15267621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:55:59  来源:igfitidea点击:

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

wpfdocumentfixeddocument

提问by nicodemus13

In WPF, in order to add a FixedPageto a FixedDocumentin code one needs to:

在WPF中,以添加FixedPageFixedDocument代码中的一个需求:

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).

  • It's ugly to have to cast to an explicit interface implementation in order to add the content to PageContent.

  • It's not simple to perform the basic operation of PageContent.

  • MSDN 文档明确指出不应这样做(“此 API 支持 .NET Framework 基础结构,不应直接从您的代码中使用。”- PageContent.IAddChild.AddChild Method)。

  • 为了将内容添加到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?

该文档实际上并未解释如何执行此操作,我找不到有关如何执行此操作的任何其他信息。还有其他方法吗?“正确”的方式?

回答by dtesenair

According to MSDN documentation, you simply add a FixedPageobject to the PageContent.Childproperty and then add that to the FixedDocumentby calling the FixedDocument.Pages.Addmethod.

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