更新:2007 年 11 月
本示例演示如何分析含有 FlowDocument 的 XAML 文件,以及如何在 FlowDocumentPageViewer 中显示所加载的文件。
示例
下面的示例定义一个命名的空 FlowDocumentPageViewer,下面的代码示例将对它进行操作。
<FlowDocumentPageViewer
Name="flowDocPageViewer"
MinZoom="50" MaxZoom="1000"
Zoom="120" ZoomIncrement="5"
/>
下面是将 FlowDocument 文件加载到 FlowDocumentPageViewer 时所涉及到的最基本的步骤:
将 FlowDocument 文件作为一个流打开。
将该流分析为 FlowDocument 对象。 此操作应当由 XamlReader 类所提供的 Load 方法来执行。
将所得到的 FlowDocument 对象设置为 FlowDocumentPageViewer 的 Document 属性值。
下面的示例执行这些步骤。
void LoadFlowDocumentPageViewerWithXAMLFile(string fileName)
{
// Open the file that contains the FlowDocument...
FileStream xamlFile = new FileStream(fileName, FileMode.Open, FileAccess.Read);
// and parse the file with the XamlReader.Load method.
FlowDocument content = XamlReader.Load(xamlFile) as FlowDocument;
// Finally, set the Document property to the FlowDocument object that was
// parsed from the input file.
flowDocPageViewer.Document = content;
xamlFile.Close();
}
如果 FlowDocument 使用相对统一资源标识符 (URI) 来引用外部资源(例如,图像文件),则必须指定一个包括 BaseUri 的 ParserContext,以便分析器能够识别相对 URI。 XamlReader 类提供可接受 ParserContext 的 Load 方法。