WPF:Visual Studio 2008 设计器中的固定文档
这是一个众所周知的错误,当您尝试在 XAML 中构造一个 FixedDocument
。例如,以下代码片段
<DocumentViewer>
<FixedDocument>
<PageContent>
<FixedPage Width="21.0cm" Height="29.7cm">
<TextBlock>Hello World!</TextBlock>
</FixedPage>
</PageContent>
</FixedDocument>
</DocumentViewer>
编译并运行得很好,但 Visual Studio 在错误列表中显示错误(属性“Pages”不支持“PageContent”类型的值。
)这非常烦人。
我正在寻找一种解决方案,允许我在 Visual Studio 中的 XAML 文件中构建文档,而不会收到该错误消息。我找到了一种解决方法,我想在下面分享它作为答案,但我很好奇是否有更好(更优雅)的解决方案。
It's a well-known bug that Visual Studio shows an error when you try to construct a FixedDocument
in XAML. 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.
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. 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
作为解决方法,我将 DocumentViewer 以及页面
进入网格:
然后我将页面附加到
Loaded
中的 DocumentViewer窗口事件:
VB 示例:
C# 示例:
As a workaround, I put the DocumentViewer as well as the page
into a grid:
Then I attach the page to the DocumentViewer in the
Loaded
event of the window:
VB example:
C# example:
我知道这个问题已经得到了回答,但我认为这个答案更好,因为它不需要您添加 DocumentView。
如果有一种方法可以通过键名称引用资源并使用 XAML 将它们放入固定文档中,请告诉我。我似乎找不到办法做到这一点,但也许这是可能的。
使用:
扩展方法:
XAML:
I know this had already been answered, but I think this answer is nicer because it doesn't require you to add a 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. I can't seem to find a way to do that, but maybe it's possible.
Use:
Extension Method:
XAML:
更简洁的解决方法:
FixedDocument
的这个子类伪造了Pages
属性,并将所有添加的页面重定向到其基类中的真实Pages
属性。用法:
将
Hillinworks.WPF.Document
更改为XamlFixedDocument
类所在的命名空间。这还可以在设计时预览文档。
A cleaner workaround:
This subclass of
FixedDocument
fakes aPages
property and redirect all added pages to the realPages
property in its base class.Usage:
Change
Hillinworks.WPF.Document
to the namespace where theXamlFixedDocument
class is located.This also enables design-time preview of your document.
所以我在弄乱固定文档时遇到了同样的问题。我认为这可能是比其他人建议的更干净的解决方法。
因此基本上您应该按照 hillin 建议创建一个从
FixedDocument
派生的自定义类,并添加一个属性从该对象的PageContents
获取FixedDocument
。但由于这些页面现在是另一个对象的可视子级,因此您应该使用 XmlReader 和 XmlWriter 类制作它们的副本。现在,在 xaml 中,您可以轻松创建一个
CustomFixedDocument
StaticResource 并将您的DocumentViewer
绑定到它的“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 getFixedDocument
from this object'sPageContents
. but since these pages are now visual children of another object you should make a copy of them using XmlReader and XmlWriter classes.now in the xaml you could easily create a
CustomFixedDocument
StaticResource and bind yourDocumentViewer
to the 'FixedDocument' property of it.Now both issues have been addressed. there is live design time preview with no compile errors and the output could be printed.