XPS ドキュメントから FixedPage オブジェクトを読み込み、処理して表示します。次のコードは、パッケージから FixedPage を読み込みます。
FixedPage fp = null;
Package package; // xps package
Uri packageUri; // uri of the package in the package store
Uri fixedPageUri; // uri of the fixed page
Dispatcher _mainDispatcher // reference to the main dispatcher, passed to this function
// this function runs in a different thread
// get the fixed page stream
Stream stream = package.GetPart(fixedPageUri).GetStream();
// create a parser context to help XamlReader with the resources used in the page
ParserContext pc = new ParserContext();
pc.BaseUri = PackUriHelper.Create(packageUri, fixedPageUri);
_mainDispatcher.BeginInvoke(
new UIRenderDelegate(delegate()
{
// this line takes its sweet time
fp = XamlReader.Load(stream, pc) as FixedPage;
stream.Dispose();
}), null).Wait();
// return the created fixed page;
ただし、その XamlReader.Load() 呼び出しには時間がかかり (特に複雑な FixedPages の場合)、UI がブロックされることがあります。独自の Dispatcher を使用して別のスレッドでこれを行うこともできますが、FixedPage クラスは freeazable ではないため、メインの UI スレッドでは使用できません。
これを回避する方法はありますか?現在、BitmapSource は Freezable であるため、RenderTargetBitmap を使用して FixedPages を画像としてレンダリングする作業に行き詰まっています。