WPF数据以异步方式绑定 [英] wpf data binding in asynchronus way

查看:45
本文介绍了WPF数据以异步方式绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF MVVM棱镜应用程序,用于上传文件.
我列出了从目录归档到数据网格中的文件,并向
上载了按钮 从datagrid上传所选文件.

为此,我将Grid的"selecteditems"传递给按钮的命令参数.
我以同步方式成功实现了此目标,现在想扩展
以异步方式实现此功能.
使用委托

这是我的异步函数调用

I have a WPF MVVM prism application to Upload files.
Me listed filed from a directory into a datagrid and an Upload button to
upload the selected files from the datagrid.

For this i pass the Grid ''selecteditems'' to the command parameter of the button.
I successfully implemented this in the synchronous way and Now wanted to extend
this functionality in asynchronous way.
using delegate

this is my asynchronous fuction call

asyncUpload.BeginInvoke(selectedFiles, out tt, new AsyncCallback(test), null);<br />


这里选择的文件是通过上载按钮的命令参数传递的数据网格中的"selecteditems".
问题是,在执行第一个上传线程时,我正在从datagrid中选择另一组文件进行上传
将更改第一个线程的选定文件.


我无法将所选文件保留在变量中.当所选内容更改时,变量值也更改了.

我该怎么解决??

在此先感谢...


Here the selected files is the ''selecteditems'' from the datagrid passed through the command parameter of the upload button.
The problem is that while executing the first thread for uploading,I''m selecting another set of files from datagrid to upload
that Will changes the first thread''s selected files.


I cant keep the selected files in a variable.when the selection changed the variable value also changed.

How can I solve this.?

Thanks in advance...

推荐答案

只是一个一般性建议:使用线程时,所有异步API都没有意义,因为它们只会使事情变得更复杂而没有任何好处.您需要在非UI线程中使用BeginInvoke,但是由于此线程中的执行不会阻塞您的UI线程,因此您始终可以执行阻塞调用Invoke.使用非UI线程的成本是使用Dispatcher.InvokeDispatcher.BeginInvoke,因为您无法从非UI线程调用UI上的任何内容,但无论如何您都在做.

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
Just a general advice: when you use threads, all asynchronous API has little sense because they only make things more complex without any benefits. You need to use BeginInvoke from a non-UI thread, but as the execution in this thread does not block your UI thread, you can always do a blocking call Invoke. The cost of using non-UI thread is using Dispatcher.Invoke or Dispatcher.BeginInvoke because you cannot call anything on UI from non-UI thread, but you''re doing it anyway.

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于WPF数据以异步方式绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆