【问题标题】:What's the correct way to add a PageContent/ FixedPage to a FixedDocument in WPF?在 WPF 中将 PageContent/FixedPage 添加到 FixedDocument 的正确方法是什么?
【发布时间】:2013-03-07 09:36:46
【问题描述】:

在 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的基本操作并不简单。

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

【问题讨论】:

    标签: wpf document fixeddocument


    【解决方案1】:

    根据 MSDN 文档,您只需将 FixedPage 对象添加到 PageContent.Child 属性,然后通过调用 FixedDocument.Pages.Add 方法将其添加到 FixedDocument

    例如:

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

    【讨论】:

    • 谢谢,文档在哪里?
    • 查看本页底部的示例:msdn.microsoft.com/en-us/library/…
    • 写入 pc.Child 时显示错误。它是一个未设置的get方法
    • MSDN 文档说PageContent.Child 属性“获取或设置 与此 PageContent 关联的 FixedPage。”这是链接:msdn.microsoft.com/en-us/library/…
    • PageContent.Child 属性在 .NET 4 中同时支持 getset,但在 .NET 3.5 中仅支持 get
    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 2016-11-09
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多