我知道這已經回答了,但我覺得這個答案是更好,因爲它不要求你添加一個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>