[英]documentviewer and backgroundworker
加載和查看我的xps文檔工作正常,但加載文檔需要很長時間,因為您知道它會導致UI看起來不活動。
XpsDocument xpsDocument = new XpsDocument(@"Resources\maintManual.xps", FileAccess.Read);
documentViewer1.Document = xpsDocument.GetFixedDocumentSequence();
所以,我試圖通過后台工作者加載文檔:
private void maintBtn_TouchDown(object sender, TouchEventArgs e) // load and view maint manual
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e) // do work
{
XpsDocument xpsDocument = new XpsDocument(@"Resources\maintManual.xps", FileAccess.Read);
documentViewer1.Document = xpsDocument.GetFixedDocumentSequence();
}
一旦“Dowork”被執行,ui“崩潰”(完全鎖定)。 我似乎無法找到原因。 有沒有更好的方法通過后台工作者調用文檔? 就像我說的那樣,Dowork括號中的代碼在后台工作者之外工作得很好,所以我知道代碼是正確的。 使用后台工作程序后,它將停止工作。 如果我沒有提供足夠的信息,請提前原諒我。 我是初級編碼員,也是這個網站的新手。
UI中的更改只能從UI線程進行。 例如,除非來自main / ui線程,否則無法訪問某些用戶控件屬性。
我建議:您的IO方法在新任務中運行,但此任務的結果適用於主線程中的UI控件。
我不知道什么是XpsDocument
,但我認為這應該工作:
private async void maintBtn_TouchDown(object sender, TouchEventArgs e) // load and view maint manual
{
XpsDocument xpsDocument = await Task.Run(() => new XpsDocument(@"Resources\maintManual.xps", FileAccess.Read));
documentViewer1.Document = xpsDocument.GetFixedDocumentSequence();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.