我正在使用.net的PrintPreviewDialog,每当它生成预览时,它会在后台锁定我的GUI并使其看起来像已经崩溃直到预览完成.看到弹出的.net页面进度窗口不是一个对话框,可以选择背景,然后以半绘制的锁定方式进入前面.当用户单击预览对话框上的实际"打印"按钮时,以及刚运行PrintDocument.Print()时,也会发生这种情况.有一种简单的方法可以修改以下代码,以便在用户等待.net绘制打印页面时停止GUI挂起:
//just showing a preview, hangs up background GUI on generating the preview
// and when user prints straight from the preview
this.printPreviewDialog.ShowDialog(this);
//just trying to print a .net PrintDocument class, GUI hangs in background
// when .net is drawing the pages
this.printDocument.Print();
Run Code Online (Sandbox Code Playgroud) 我需要使用诸如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)