如何:将 XAML 文件加载到 FlowDocumentPageViewer 中

更新:2007 年 11 月

本示例演示如何分析含有 FlowDocument 的 XAML 文件,以及如何在 FlowDocumentPageViewer 中显示所加载的文件。

示例

下面的示例定义一个命名的空 FlowDocumentPageViewer,下面的代码示例将对它进行操作。

<FlowDocumentPageViewer
  Name="flowDocPageViewer" 
  MinZoom="50" MaxZoom="1000"
  Zoom="120" ZoomIncrement="5"
  />

下面是将 FlowDocument 文件加载到 FlowDocumentPageViewer 时所涉及到的最基本的步骤:

  1. FlowDocument 文件作为一个流打开。

  2. 将该流分析为 FlowDocument 对象。 此操作应当由 XamlReader 类所提供的 Load 方法来执行。

  3. 将所得到的 FlowDocument 对象设置为 FlowDocumentPageViewerDocument 属性值。

下面的示例执行这些步骤。

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) 来引用外部资源(例如,图像文件),则必须指定一个包括 BaseUriParserContext,以便分析器能够识别相对 URI。 XamlReader 类提供可接受 ParserContextLoad 方法。

请参见

任务

如何:将 FlowDocumentPageViewer 的内容另存为 XAML 文件