如何从Dispatcher线程访问单独的线程生成的WPF UI元素? [英] How to access separate thread generated WPF UI elements from the Dispatcher thread?

查看:211
本文介绍了如何从Dispatcher线程访问单独的线程生成的WPF UI元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用wpf UI元素(如FixedDocument,FlowDocument,PageContent,BlockUIContainer等)来生成打印预览(很长).为了保持我的UI响应能力,我在一个单独的Thread类线程上做这部分(BackgroundWorker无法工作,因为我需要一个STA线程).到目前为止,一切正常.
但是,现在显示打印预览之后,我需要打印,然后在生成的预览上单击打印"图标会引发臭名昭著的调用线程无法访问该对象,因为其他线程拥有它."例外.那么,有什么办法吗?

I need to generate a print preview (a long one) using wpf UI elements like FixedDocument, FlowDocument, PageContent, BlockUIContainer and all those. To keep my UI responsive i'm doing this part on a separate Thread class thread (BackgroundWorker won't work since i need an STA thread). Everything is OK upto this point.
But after displaying the print preview now i need to print, and clicking Print icon on the generated preview throws the infamous "The calling thread cannot access this object because a different thread owns it." exception. So, is there any way around?

编辑(代码):

Dispatcher.CurrentDispatcher.Invoke(new Action(() =>  
    {  
        Thread thread = new Thread(() =>  
            {  
                FixedDocument document = renderFlowDocumentTemplate(report);  
                PrintPreview preview = new PrintPreview();  
                preview.WindowState = WindowState.Normal;  
                preview.documentViewer.Document = document;  
                preview.ShowDialog();  
            });  
        thread.SetApartmentState(ApartmentState.STA);  
        thread.Start();  
    }));`

好的,在这里,RenderFlowDocumentTemplate()生成打印预览(包含UI元素),并用Report数据填充它们. PrintPreview是一个自定义窗口,其中包含一个DocumentViewer元素,该元素实际上保存并显示预览,并且包含打印"图标,如果单击该图标,我将获得打印对话框"窗口.

Ok here, the RenderFlowDocumentTemplate() generates the print preview (which contains the UI elements) and fills the them with Report data. PrintPreview is a custom window that contains a DocumentViewer element that actually holds and displays the preview, and contains the Print icon, upon clicking which i'm supposd to get the PrintDialog window.

编辑(XAML):

<cw:CustomWindow x:Class="MyApp.Reports.PrintPreview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cw="clr-namespace:MyApp.UI.CustomWindows;assembly=MyApp.UI.CustomWindows">    
    <DocumentViewer Margin="0,30,0,0" Name="documentViewer"></DocumentViewer>
</cw:CustomWindow>`

推荐答案

找到了另一个问题完全相同的人-此处是真正的救星. 现在,我不再尝试从Dispatcher线程访问由辅助线程生成的UI元素,而是现在在辅助线程上执行其余的打印过程. UI元素没有跨线程的"VerifyAccess",并且工作正常. :)

Found another guy with exactly the same problem - Printing the content of a DocumentViewer in a different UI thread. Just followed the same path. The code here was a real savior.
Now I'm NOT trying to access the secondary thread generated UI element from the Dispatcher thread, instead now the rest of the printing procedure is executed on the secondary thread. No cross-thread "VerifyAccess" of UI elements, and It's working smoothly. :)

这篇关于如何从Dispatcher线程访问单独的线程生成的WPF UI元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆