ati*_*yar 5 wpf multithreading dispatcher print-preview
我需要使用诸如FixedDocument,FlowDocument,PageContent,BlockUIContainer等所有wpf UI元素生成打印预览(长篇).为了保持我的UI响应,我在一个单独的Thread类线程上做这个部分(BackgroundWorker将无法工作,因为我需要一个STA线程).到目前为止一切都很好.
但是在显示打印预览之后我现在需要打印,并且在生成的预览上单击"打印"图标会引发臭名昭着的"调用线程无法访问此对象,因为其他线程拥有它".例外.那么,有什么办法吗?
编辑(代码):
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();
}));`
Run Code Online (Sandbox Code Playgroud)
好的,RenderFlowDocumentTemplate()生成打印预览(包含UI元素)并用报表数据填充它们.PrintPreview是一个自定义窗口,其中包含一个实际保存并显示预览的DocumentViewer元素,并包含打印图标,点击后我想要获取PrintDialog窗口.
编辑(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>`
Run Code Online (Sandbox Code Playgroud)
发现另一个人有完全相同的问题 -在不同的 UI 线程中打印 DocumentViewer 的内容。只是走同样的路。这里的代码是一个真正的救星。
现在我不尝试从调度程序线程访问辅助线程生成的 UI 元素,而是现在打印过程的其余部分在辅助线程上执行。UI 元素没有跨线程“VerifyAccess”,并且工作顺利。:)
| 归档时间: |
|
| 查看次数: |
3482 次 |
| 最近记录: |