HOW TO:將 XAML 檔載入 FlowDocumentReader
更新:2007 年 11 月
這個範例示範如何剖析包含 FlowDocument 的 XAML 檔案,並顯示 FlowDocumentReader 中載入的檔案。
範例
下列範例會定義一個供下列程式碼範例操作的空白具名 FlowDocumentReader。
<FlowDocumentReader
Name="flowDocRdr"
IsFindEnabled="True"
IsPrintEnabled="True"
MinZoom="50" MaxZoom="1000"
Zoom="120" ZoomIncrement="5"
/>
在最基本的層級中,有將 FlowDocument 檔案載入至 FlowDocumentReader 所需的步驟。
將 FlowDocument 檔案當做串流開啟。
將串流剖析至 FlowDocument 物件中。XamlReader 類別提供的 Load 方法是供執行這項操作之用。
設定產生的 FlowDocument 物件做為 FlowDocumentReader 上 Document 屬性的值。
下列範例會執行這些步驟。
void LoadFlowDocumentReaderWithXAMLFile(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.
flowDocRdr.Document = content;
xamlFile.Close();
}
如果 FlowDocument 會使用相對的統一資源識別元 (URI) 來參考外部資源 (例如,影像檔),則必須指定包含 BaseUri 的 ParserContext,如此剖析器 (Parser) 才能了解相對 URI 的意思。XamlReader 類別會提供 Load 方法,這個方法會接受 ParserContext。
如需讓使用者可以將 XAML 檔載入至 FlowDocumentReader 的功能範例,請參閱 FlowDocumentReader 載入/儲存 XAML 範例。