how do you implement Async Operations in C# and MVVM?

后端 未结 3 1118
甜味超标
甜味超标 2021-02-04 14:28

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 wh

3条回答
  •  别那么骄傲
    2021-02-04 14:52

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

    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.

    To summarize:

    • 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.

提交回复
热议问题