How to: Load a XAML File into a FlowDocumentPageViewer
This example demonstrates how to parse a XAML file that contains a FlowDocument, and display the loaded file in a FlowDocumentPageViewer.
Example
The following example defines an empty, named FlowDocumentPageViewer that will be manipulated by the code example below.
<FlowDocumentPageViewer
Name="flowDocPageViewer"
MinZoom="50" MaxZoom="1000"
Zoom="120" ZoomIncrement="5"
/>
At the most basic level, there are steps involved in loading a FlowDocument file into a FlowDocumentPageViewer.
Open the FlowDocument file as a stream.
Parse the stream into a FlowDocument object. The Load method provided by the XamlReader class is intended to perform this operation.
Set the resulting FlowDocument object as the value of the Document property on the FlowDocumentPageViewer.
The following example performs these steps.
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();
}
If the FlowDocument references external resources (such as image files) using relative uniform resource identifiers (URIs), it is necessary to specify a ParserContext that includes a BaseUri so that the parser can make sense of the relative URIs. The XamlReader class provides Load method that accepts a ParserContext.
See Also
Tasks
How to: Save the Contents of a FlowDocumentPageViewer as a XAML File