Live Chat Icon For mobile
Live Chat Icon

How do I load a FlowDocument XAML content from outside the application directory?

Platform: WPF| Category: Flow Content

Let us say you have your application running in a folder called C:\MyApp. Then if you want to load a FlowDocumentContent.xaml from a folder called C:\Contentthen this is easily done by first defining a FlowDocumentReader in your app like so:

 [XAML]
              <FlowDocumentReader Name='myFlowDocument' ViewingMode='Scroll'  IsTwoPageViewEnabled='False' IsPageViewEnabled='False'>
              </FlowDocumentReader>

And load the xaml file as follows:

 [C#]
            	FileStream xamlFile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
	// This will work, only if the top-level element in the document is FlowDocument
            	FlowDocument content = XamlReader.Load(xamlFile) as FlowDocument;

	this.myFlowDocument.Document = content;

            	xamlFile.Close();

But if you have references to sub-folders in your document. For example, if you are referring to images under this folder C:\Content\Images, then those images wouldn’t be found by the parser because the parser is executing with the application directory context whereas the document contents are in a different directory. To make this work, you can force a context on the parser as follows:

 [C#]
            	ParserContext context = new ParserContext();
            	context.BaseUri = new Uri(filePath, UriKind.Absolute);
            	FlowDocument content = XamlReader.Load(xamlFile, context) as FlowDocument;

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.