下载开始时 WPF 窗口未完成渲染
我正在编写一个小文件下载实用程序。 DownloadFile() 方法在我的 MainWindow 的 Window_Loaded() 事件上调用。由于 DownloadFile 方法是资源密集型的,因此当下载开始时,MainWindow 不会完成在屏幕上的渲染。下载完成后,我就看到了 WPF 窗体上的实际控件。
为了控制这个,我使用了以下 DoEvents() 代码,但它仍然不起作用。我在 Form 构造函数中的 InitializeComponent() 之后、在 Window_Loaded 事件中调用 DownloadFile() 之前调用此函数。
private void DoEvents()
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
}
I am writing a small file download utility. The DownloadFile() method is called on Window_Loaded() event of my MainWindow. Since the DownloadFile method is resource intensive, the MainWindow does not finish rendering on screen when the download starts. It is just after the download is finished that I come to see the actual controls on my WPF Form.
To control this, I am using following DoEvents() code, but still it is not working. I am calling this function after InitializeComponent() in the Form constructor and just before calling DownloadFile() in the Window_Loaded event.
private void DoEvents()
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 DownloadFileAsync 方法 。
Have a look at the DownloadFileAsync method.
始终建议在单独的线程中完成所有服务器访问并下载相关内容。您可以在以下链接中查看如何操作。
http://bathinenivenkatesh.blogspot.com/2011/07 /wpf-build-more-responsive-ui.html
it is always suggested to do all the server hits and download related things in a separate thread. You can see how to that in following link.
http://bathinenivenkatesh.blogspot.com/2011/07/wpf-build-more-responsive-ui.html
您应该使用 BeginInvoke(使用“后台”选项)并将 DownloadFile 放在那里。
You should use BeginInvoke instead (with the Background option) and put the DownloadFile there.