开发者

WPF: Transfer non-freezable items from one Dispatcher to another

We load FixedPage objects from an XPS document, process and displays it.The following code does the loading of the FixedPage from a Package:

        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

        // g开发者_如何学JAVAet 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;

However, that XamlReader.Load() call takes a long time (especially for complex FixedPages) and sometimes blocks the UI. We could do this in another thread with it's own Dispatcher, but since the FixedPage class is not freeazable, its can't be used by the main UI thread.

Is there a way around this? Right now we're stuck rendering the FixedPages as images using RenderTargetBitmap since BitmapSource is Freezable.


Load the xaml into a memory stream first and them pass the memory stream to the UI thread. Parse the memorystream on the UI thread. This way you could do the slow file IO on he background thread. Only the inmemeory parsing of the stream (using XamlReader.Load) would be done on the UI Thread.

This artice explains how to do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜