내 WPF 응용 프로그램에는 다른 컨트롤 중에서 DocumentViewer
이 포함 된 특정 Window
이 있습니다.다른 UI 스레드에서 DocumentViewer의 내용 인쇄
이 창을 열고로드하면 진행률 표시기가있는 FixedDocument
을 동적으로 빌드 한 다음 DocumentViewer
에 표시합니다. 그것은 작동하고 사용자 경험을 향상시키기 위해이 창을 자체 스레드에서 실행하므로 문서를 작성하는 동안 주 응용 프로그램 창이 계속 응답합니다.
public void ShowDocumentViewerWindow(params object[] data) {
var thread = new Thread(() => {
var window = new MyDocumentViewerWindow(new MyObject(data));
window.Closed += (s, a) => window.Dispatcher.InvokeShutdown();
window.Show();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
지금까지이 설정과 행복,하지만 난 그냥 문제로 실행 : this web page에서 팁을 바탕으로
는이 같은 새 스레드 내 창을 엽니 다. 나는 그것의 자신의 스레드에서 창을 가지고 전에
<Button Command="Print" CommandTarget="{Binding ElementName=MyDocumentViewer}">Print</Button>
, 이것은 잘 작동 :
MyDocumentViewerWindow
System.Windows.Threading.Dispatcher.Run();
스택 추적은 다음과 같이 시작합니다
at System.Windows.Threading.Dispatcher.VerifyAccess()
at MS.Internal.Printing.Win32PrintDialog.ShowDialog()
at System.Windows.Controls.PrintDialog.ShowDialog()
at System.Printing.PrintQueue.GatherDataFromPrintDialog(PrintDialog printDialog, XpsDocumentWriter&amp; writer, PrintTicket&amp; partialTrustPrintTicket, PrintQueue&amp; partialTrustPrintQueue, Double&amp; width, Double&amp; height, String jobDescription)
at System.Printing.PrintQueue.CreateXpsDocumentWriter(String jobDescription, PrintDocumentImageableArea&amp; documentImageableArea)
at System.Windows.Controls.Primitives.DocumentViewerBase.OnPrintCommand()
at System.Windows.Controls.Primitives.DocumentViewerBase.ExecutedRoutedEventHandler(Object target, ExecutedRoutedEventArgs args)
...
내 직감은 인쇄 대화 상자가 메인 UI 스레드에서 개방하고, 생성 된 문서에 액세스를 시도하고 소유한다는 것입니다 내 자신의 스레드에 의해, 따라서 충돌.
어떻게 해결할 수 있을까요? 창을 자체 스레드에 보관하고 싶습니다.
감사합니다. 내 문서를 만드는 것은 FixedDocument 인스턴스화, FixedPage 개체 추가, 컨트롤로 채우기 등을 포함합니다. FixedDocument가 DispatcherObject이기 때문에 백그라운드 스레드에서이 인스턴스를 만든 다음 DocumentViewer의 소스로 설정할 수 없었습니다. 크로스 스레드 위반 내 문서를 DocumentViewer와 동일한 스레드 (즉, UI 스레드)에 작성해야한다는 것을 알게되었습니다 .-(하지만 지금은 문제에 대한 해결책을 찾았습니다. 지금 게시 할 것입니다. – Ross