简体   繁体   English

WPF:Visual Studio 2008 设计器中的固定文档

[英]WPF: FixedDocument in Visual Studio 2008 Designer

It's a well-known bug that Visual Studio shows an error when you try to construct a FixedDocument in XAML.当您尝试在 XAML 中构造FixedDocument时,Visual Studio 会显示错误,这是一个众所周知的错误 For example, the following snippet例如,以下代码段

<DocumentViewer>
    <FixedDocument>
        <PageContent>
            <FixedPage Width="21.0cm" Height="29.7cm">
                <TextBlock>Hello World!</TextBlock>
            </FixedPage>
        </PageContent>
    </FixedDocument>
</DocumentViewer>

compiles and runs perfectly fine, but Visual Studio shows an error in the error list ( Property 'Pages' does not support values of type 'PageContent'. ) This is quite annoying.编译并运行得很好,但 Visual Studio 在错误列表中显示错误( Property 'Pages' does not support values of type 'PageContent'. )这很烦人。

I'm looking for a solution that allows me to construct my documents in a XAML file in Visual Studio without getting that error message.我正在寻找一种解决方案,它允许我在 Visual Studio 的 XAML 文件中构建我的文档,而不会收到该错误消息。 I've found a workaround, which I'd like to share below as an answer, but I'm curious if there's a better (more elegant) solution around.我找到了一个解决方法,我想在下面分享作为答案,但我很好奇是否有更好(更优雅)的解决方案。

As a workaround, I put the DocumentViewer as well as the page into a grid:作为一种解决方法,我将 DocumentViewer 以及页面放入网格中:

<Grid>
    <FixedPage Width="21.0cm" Height="29.7cm" x:Name="uiPage1">
        <TextBlock>Hello World!</TextBlock>
    </FixedPage>
    <DocumentViewer>
        <FixedDocument x:Name="uiReport">
        </FixedDocument>
    </DocumentViewer>
</Grid>

Then I attach the page to the DocumentViewer in the Loaded event of the window:然后我在窗口的Loaded事件中将页面附加到 DocumentViewer:

VB example: VB示例:

DirectCast(Me.uiPage1.Parent, Grid).Children.Remove(Me.uiPage1)
Dim content As New PageContent()
DirectCast(content, IAddChild).AddChild(Me.uiPage1)
Me.uiReport.Pages.Add(content)

C# example: C# 示例:

((Grid)uiPage1.Parent).Children.Remove(uiPage1);
var content = new PageContent();
((IAddChild)content).AddChild(uiPage1);
uiReport.Pages.Add(content);

A cleaner workaround:更清洁的解决方法:

[ContentProperty("Pages")]
public class XamlFixedDocument : FixedDocument
{
    private ObservableCollection<PageContent> _pages;

    public XamlFixedDocument()
    {
        this.Pages = new ObservableCollection<PageContent>();
    }

    public new ObservableCollection<PageContent> Pages
    {
        get => _pages;
        set
        {
            _pages = value;

            foreach (var page in _pages)
            {
                base.Pages.Add(page);
            }

            _pages.CollectionChanged += (o, e) =>
            {
                if (e.NewItems != null)
                {
                    foreach (PageContent page in e.NewItems)
                    {
                        base.Pages.Add(page);
                    }
                }
            };
        }
    }
}

This subclass of FixedDocument fakes a Pages property and redirect all added pages to the real Pages property in its base class. FixedDocument这个子类伪造Pages属性并将所有添加的页面重定向到其基类中的真实Pages属性。

Usage:用法:

<doc:XamlFixedDocument xmlns:doc="clr-namespace:Hillinworks.WPF.Document">
    <PageContent>
        <FixedPage Background="White">
            <TextBlock Text="hello, world" />
        </FixedPage>
    </PageContent>
</doc:XamlFixedDocument>

Change Hillinworks.WPF.Document to the namespace where the XamlFixedDocument class is located.Hillinworks.WPF.Document更改为XamlFixedDocument类所在的命名空间。

This also enables design-time preview of your document.这还可以启用文档的设计时预览。

I know this had already been answered, but I think this answer is nicer because it doesn't require you to add a DocumentView.我知道这已经得到了回答,但我认为这个答案更好,因为它不需要您添加 DocumentView。

If there's a way to reference the resources by the key name and put them in the FixedDocument with XAML, please let me know.如果有办法通过键名引用资源并使用 XAML 将它们放在 FixedDocument 中,请告诉我。 I can't seem to find a way to do that, but maybe it's possible.我似乎无法找到一种方法来做到这一点,但也许这是可能的。

Use:用:

var doc = System.Windows.Application.LoadComponent(new Uri("/FixedDocumentExample.xaml", UriKind.Relative)) as FixedDocument;
doc.AddPages();

Extension Method:扩展方法:

using System.Collections;
using System.Windows.Documents;

public static class FixedDocumentExtended {
    public static void AddPages(this FixedDocument fixedDocument) {
        var enumerator = fixedDocument.Resources.GetEnumerator();
        while (enumerator.MoveNext()) {
            var pageContent = ((DictionaryEntry)enumerator.Current).Value as PageContent;
            if (pageContent != null) {
                fixedDocument.Pages.Add(pageContent);
            }
        }
    }
}

XAML: XAML:

<FixedDocument
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FixedDocument.Resources>
        <PageContent x:Key="page1">
            <FixedPage Width="793.76" Height="1122.56">
                <TextBlock Margin="50" Text="Page 1"/>
            </FixedPage>
        </PageContent>
        <PageContent x:Key="page2">
            <FixedPage Width="793.76" Height="1122.56">
                <TextBlock Margin="50" Text="Page 2"/>
            </FixedPage>
        </PageContent>
    </FixedDocument.Resources>
</FixedDocument>

So I was messing with fixed documents and I came across the same problem.所以我弄乱了固定文件,我遇到了同样的问题。 and I think this is maybe even a cleaner workaround than what others have suggested.我认为这可能是比其他人建议的更简洁的解决方法。

So basically you should create a custom class derived from FixedDocument as hillin suggested, and add a property to get FixedDocument from this object's PageContents .因此,基本上您应该按照hillin 的建议创建一个从FixedDocument派生的自定义类,并添加一个属性以从此对象的PageContents获取FixedDocument but since these pages are now visual children of another object you should make a copy of them using XmlReader and XmlWriter classes.但由于这些页面现在是另一个对象的可视子页面,您应该使用 XmlReader 和 XmlWriter 类制作它们的副本。

[ContentProperty("Pages")]
public class CustomFixedDocument : FixedDocument
{
    private ObservableCollection<PageContent> _pages;

    public CustomFixedDocument()
    {
        this.Pages = new ObservableCollection<PageContent>();
    }

    public FixedDocument FixedDocument
    {
        get
        {
            var document = new FixedDocument();
            foreach (var p in Pages)
            {
                var copy = XamlReader.Parse(XamlWriter.Save(p)) as PageContent;
                document.Pages.Add(copy);
            }
            return document;
        }
    }

    public new ObservableCollection<PageContent> Pages
    {
        get => _pages;
        set
        {
            _pages = value;

            foreach (var page in _pages)
            {
                base.Pages.Add(page);
            }

            _pages.CollectionChanged += (o, e) =>
            {
                if (e.NewItems != null)
                {
                    foreach (PageContent page in e.NewItems)
                    {
                        base.Pages.Add(page);
                    }
                }
            };
        }
    }
}

now in the xaml you could easily create a CustomFixedDocument StaticResource and bind your DocumentViewer to the 'FixedDocument' property of it.现在在 xaml 中,您可以轻松创建CustomFixedDocument StaticResource 并将您的DocumentViewer绑定到它的“FixedDocument”属性。

<Window x:Class="MyProject.DocumentWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MyProject"
    mc:Ignorable="d"
    Title="DocumentWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
    <local:CustomFixedDocument x:Key="Report">
        <PageContent>
            <FixedPage Width="793.76" Height="1122.56">
                <TextBlock Margin="50" Text="Page 1"/>
            </FixedPage>
        </PageContent>
        <PageContent>
            <FixedPage Width="793.76" Height="1122.56">
                <TextBlock Margin="50" Text="Page 2"/>
            </FixedPage>
        </PageContent>
    </local:CustomFixedDocument>
</Window.Resources>
<Grid>
    <DocumentViewer x:Name="viewer" Document="{Binding Source={StaticResource Report}, Path=FixedDocument}"/>
</Grid>

Now both issues have been addressed.现在这两个问题都得到了解决。 there is live design time preview with no compile errors and the output could be printed.有实时设计时预览,没有编译错误,并且可以打印输出。

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

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