I fail to get back on the UI thread using Reactive Extentions and the MVVM Light Toolkit.
I got the follow properties on a ViewModel:
private ObservableCollection<Workspace> _workspaces;
public ObservableCollection<Workspace> Workspaces
{
get
{
return _workspaces;
}
set
{
if (_workspaces == value)
return;
_workspaces = value;
RaisePropertyChanged("WorkspacesLoaded");
}
}
public bool WorkspacesLoaded
{
get
{
if (_workspaces != null && _workspaces.Count > 0)
return true;
return false;
}
}
Notice the RaisePropertyChanged("WorkspacesLoaded"); which never seems to arrive at the UI.
The following code subscribes to a Subject<Workspace> and from what I read the .ObserveOnDispatcher() should bring me back on the UI Thread.
_workspaceSubscription = _workspaceSource.WorkspaceMutations
.ObserveOnDispatcher()
.SubscribeOn(Scheduler.ThreadPool)
.Subscribe(OnNewWorkspaceMutation, OnWorkspaceMutationError);
_workspaceSource.GetWorkspaces();
And this code adds to the Workspaces property:
private void OnWorkspaceCreated(Workspace workspace)
{
Workspaces.Add(workspace);
}
This is what baffles me: The Workspaces collection gets properly populated and the UI binds to it properly. However, when I set a breakpoint on the setter of Workspaces it never gets hit so my suspicion is that the Observable stream isn’t returned to the UI thread.
Any help would be highly appreciated.
It’s because you add to the Workspaces collection, you don’t actually set the collection property again. If you have used
then the setting of the wokrspace property would have been triggered. Now it just uses the value that is already there. If you set a breakpoint in the getter it will be hit.