我知道这已经回答了,但我觉得这个答案是更好,因为它不要求你添加一个DocumentView。
如果有一种方法可以通过密钥名称引用资源并将它们放入带有XAML的FixedDocument中,请让我知道。我似乎无法找到办法做到这一点,但也许这是可能的。
用途:
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:
<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>