0

I saw an original question here. asynchronous UI update from ViewModel in WPF Although there was an accepted answer, the original author was asking a question I'm stumped on. This all boils down to the proper way to incorporate the Dispatcher object in a bound View model.

So...I have a view with a grid bound to a observable collection in a View Model "Cars". In the constructor of the view model I call SharePoint using QueryAsync. In the success method I'm supposed to call the Dispatcher. Possibles are >> Dispatcher.CurrentDispatcher.Invoke(),App.Current.Dispatcher.Invoke

None of these seem to work for me. I don't know if I'm supposed to send the Dispatcher to the View Model. (I don't know how to do that) or Create a Dispatcher in the View Model and assign it something.

I really don't even think I'm doing this the right way. You would think there would be some decent example code out there.

Community
  • 1
  • 1
  • What do you mean by 'none of these seem to work for me'? Are you using silverlight or WPF? Check if this answer helps: http://stackoverflow.com/a/4621651/667792 – Miłosz Wierzbicki Mar 01 '15 at 21:31
  • As Milosz said, please expand on why what you are trying isn't working. i.e. any error message you are getting. It is would also be helpful to know if this is actualy WPF or silverlight; you have both tagged and they often do things slightly differently. In either case if you do `Dispatcher.BeginInvoke(() => { })` it should work. But i would also do a `Dispatcher.CheckAccess()` first and only call the invoke if the check fails. If the check comes back true, just call the action by itself. – Taekahn Mar 02 '15 at 00:13

1 Answers1

1

You could add a public reference to the Dispatcher of the UI Root Visual (your main window/user control) in the App class (App.xaml.cs), and possibly inject it when instancing your view model.

I suggest you to use a MVVM framework like MVVM Ligh toolkit, that already solves the problem in a very elegant way, here an example: Simple example of DispatcherHelper

Community
  • 1
  • 1
SalvadorGomez
  • 552
  • 4
  • 15