简体   繁体   English

documentviewer和backgroundworker

[英]documentviewer and backgroundworker

Loading and viewing my xps document works just fine but it takes a long time to load the document and as you know it causes the UI to look inactive. 加载和查看我的xps文档工作正常,但加载文档需要很长时间,因为您知道它会导致UI看起来不活动。

XpsDocument xpsDocument = new XpsDocument(@"Resources\maintManual.xps", FileAccess.Read);  
documentViewer1.Document = xpsDocument.GetFixedDocumentSequence();

So, I am trying to load the document through the backgroundworker: 所以,我试图通过后台工作者加载文档:

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();   
}

As soon as the "Dowork" is executed the ui "crashes" (locks up totally). 一旦“Dowork”被执行,ui“崩溃”(完全锁定)。 I can't seem to find why. 我似乎无法找到原因。 Is there a better way to call the document via the background worker? 有没有更好的方法通过后台工作者调用文档? Like I said, the code in the Dowork brackets works just fine outside of the background worker on it's own, so I know the code is right. 就像我说的那样,Dowork括号中的代码在后台工作者之外工作得很好,所以我知道代码是正确的。 Once the background worker is used, it stops working. 使用后台工作程序后,它将停止工作。 Please forgive me in advance if I didn't include enough information. 如果我没有提供足够的信息,请提前原谅我。 I am junior coder and new to this site. 我是初级编码员,也是这个网站的新手。

Changes in UI are possible only from UI thread. UI中的更改只能从UI线程进行。 You cannot access to to some property of user control, for instance, unless from main/ui thread. 例如,除非来自main / ui线程,否则无法访问某些用户控件属性。

What I would recommend: Your IO method run in new Task, but result of this task apply to your UI control in main thread. 我建议:您的IO方法在新任务中运行,但此任务的结果适用于主线程中的UI控件。

I don't know what is XpsDocument , however I suppose this should work: 我不知道什么是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM