2

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 を画像としてレンダリングする作業に行き詰まっています。

4

1 に答える 1

-1

最初に xaml をメモリ ストリームにロードし、メモリ ストリームを UI スレッドに渡します。UI スレッドでメモリストリームを解析します。このようにして、バックグラウンド スレッドで低速のファイル IO を実行できます。ストリームのメモリ内解析 (XamlReader.Load を使用) のみが UI スレッドで実行されます。

この記事では、その方法について説明します。

于 2010-02-25T13:31:57.393 に答える