你如何在C#和MVVM实现异步操作?

编程入门 行业动态 更新时间:2024-10-28 13:14:44
本文介绍了你如何在C#和MVVM实现异步操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

喜的是落实在WPF和MVVM非同步操作最简单的方法,让我们说,如果用户如果用户点击进入时,在现场我现在要启动一个命令,然后恢复,而线程会做一些搜索操作,然后回来更新属性,以便通知可以更新绑定。

hi what is the easiest way to implement asynch operations on WPF and MVVM, lets say if user if user hits enter when on a field i want to launch a command and then return back while a thread will do some search operations and then come back and update the properties so notification can update the bindings.

谢谢!

推荐答案

怎么样的 BackgroundWorker的实例在虚拟机上打电话给你的命令?

How about a BackgroundWorker instance to call your command on the VM ?

更新:刮开上面的建议..有一个关于MVVM在线视频贾森Dolinger ..我建议你看一看这一点。这是其中的观点是薄更清洁的方式/并无持有任何线程code。

Update: Scratch the above suggestion.. There's an online video on MVVM by Jason Dolinger.. I recommend you take a look at that. It's a cleaner way where the view is thin/ does not hold any threading code.

要总结一下:

  • 的VM构造函数缓存Dispatcher.CurrentDispatcher对象(主线程)。
  • 更新后备存储(结果)时,使用 _dispatcher.BeginInvoke(()=> _results.AddRange(项))。,使得UI正确更新
  • the VM ctor caches the Dispatcher.CurrentDispatcher object (main thread).
  • when updating the backing store (results), use _dispatcher.BeginInvoke( () => _results.AddRange( entries) ) so that the UI is updated correctly.

更多推荐

你如何在C#和MVVM实现异步操作?

本文发布于:2023-10-30 03:03:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   如何在   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!