WPF:固定文档在Visual Studio 2008设计 [英] WPF: FixedDocument in Visual Studio 2008 Designer
问题描述
这是 是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>
编译和运行完全正常,但Visual Studio中显示错误列表中的错误(属性页不支持类型PageContent的值。
)这是很烦人的。
我正在寻找一个解决方案,让我建立我在Visual Studio中的XAML文件,文件没有得到该错误消息。我已经找到了解决办法,我想作为一个答案下面分享,但我很好奇,如果有围绕一个更好的(更优雅)的解决方案。
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.
推荐答案
我知道这已经回答了,但我想这个答案是更好,因为它不要求你添加一个DocumentView。
I know this had already been answered, but I think this answer is nicer because it doesn't require you to add a DocumentView.
如果有一种方法通过密钥名称引用的资源,并把它们与XAML的固定文档,请让我知道。我似乎无法找到一个方法来做到这一点,但也许这是可能的。
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.
使用:
var doc = System.Windows.Application.LoadComponent(new Uri("/FixedDocumentExample.xaml", UriKind.Relative)) as FixedDocument;
doc.AddPages();
扩展方法:
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>
这篇关于WPF:固定文档在Visual Studio 2008设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!