I'm supposed to be able to access the Dispatcher that belongs to the View I need to pass it to the ViewModel. But the View should not know anything about the ViewModel, so how do you pass it? Introduce an interface or instead of passing it to the instances create a global dispatcher singleton that will be written by the View? How do you solve this in your MVVM applications and frameworks?
EDIT: Note that since my ViewModels might be created in background threads I can't just do Dispatcher.Current
in the constructor of the ViewModel.
Maybe I am a bit late to this discussion, but I found 1 nice article https://msdn.microsoft.com/en-us/magazine/dn605875.aspx
There is 1 paragraph
Assume if you can use async/await properly, this is not an issue.
Another common pattern (which is seeing much use now in the framework) is the SynchronizationContext.
It enables you to dispatch synchronously and asynchronously. You can also set the current SynchronizationContext on the current thread, meaning it is easily mocked. The DispatcherSynchronizationContext is used by WPF apps. Other implementations of the SynchronizationContext are used by WCF and WF4.
You don't need to pass the UI Dispatcher to the ViewModel. The UI Dispatcher is available from the current application singleton.
This will make your ViewModel dependent on the View. Depending on your application, that may or may not be fine.