Create new view via MediatorMessagesink Dispatcher problem

Nov 28, 2011 at 11:57 PM

Hi Sacha,

I am going through your articles and demo projects for Cinch v2 for our WPF project and find Cinch v2 a great tool to use. Thanks for your hardworking and amazing work!

I am follwing a simple demo using Cinch v2 and trying out MediatorSinkMessage between/among Views. But I am getting an error when trying to add a new Workspacedata to ViewModelBase.Views collection in MainWindowViewModel via MediatorMessageSink call. This may be the issue which have already been talked about or resolved, but I can't seem to find any references...

An exception thrown is NotSupportException, "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread."
The error is probably WPF generic error on ObservableCollection as ObservableCollection doesn't support changes to SourceCollection from a different thread. I like the idea of Cinch Workspace data and of using Collection to keep track of available views in MainWindowView (just like your demo).

Your demo seems to create views in static way, on ViewAwareStatus ViewLoaded event. Our project oftern involves creating/adding new Views at runtime. Hence, I'd like to have a common method to add a new Workspacedata to Views in MainWindowViewModel and have it called via MessageSink.

In demo, I have:
- MainWindowView which inherits from Cinch.ViewModelBase and contains TabControlEx bound to Cinch out-of-box 'Views' collection
- HomeView which contains a button 'Create New View' and which is created as the first Workspacedata item on ViewAwareStatusService ViewLoaded event in MainWindowViewModel
- BlahView which simply displays some text (e.g. "blah blah blah blah")

- MainWindowViewModel has some method, say 'OnCreateNewBlahView' with MediatorMessageSinck "CreateNewBlahViewMessage". Within the method, all it does is to create a new Workspacedata (null, "BlahView", null, "New Blah", true), and add it to Views

- HomeViewModel has a method 'ExecuteCreateNewViewCommand' simply to send a message back to MainWindowViewModel by notifying

When NotifyCollegues is called from HomeViewModel, the line in MediatorSingleton.cs gets the error:

            foreach (var cb in wrCopy)
                Delegate action = cb.GetMethod();
                if (action != null)
DynamicInvoke method invokes the common method 'OnCreateNewBlahView' in MainWindowViewModel, but ObservableCollectio Cinch.Views doesn't like to be modified at this point.

Is there better way to handle runtime creation of Workspacedata via MediatorMessageSink to Views? I'd prefer not to rewrite ObservableCollection 'Views' (and functionality) to something like multi-threading ObservableCollection (e.g. your DispatcherNotifiedObservableCollection), if I can avoid it.

Any suggestions very much appreciated

Nov 29, 2011 at 2:57 PM

Yeah I have seen something like this once. We have also had issues at work from time to time, using ObservableCollection. Problem is, its so convenient to use  ObservableCollection. I think overall could I do things over again, I would use standard List, which I would remove items from, and then just raise INPC change notification for. But I do not know how many people I would break code for, so I have to leave it as is. If you are (and you are only 2nd person ever to report this, and I fixed the 1st issue, so that only leaves you) need that functionality I would create your own ViewModel WPF code based on Cinch where you swap out ObservableCollection to standard List<T>, or download Cinch code and change it, and compile it how you want to use it.


Sorry but I could end up breaking things for a lot more people if I change something like that.


PS : Glad you like Cinch

Nov 29, 2011 at 8:06 PM

Thanks for your quick reply. I wouldn't like to impose on you to change code just because of this. It'd be just too much hassle. I will create some custom ViewModelBase based on Cinch one.
I was hoping to have work-around or something, and was feeling a little guilty about changing someone's code when it's working well, although it's open-source...

Thanks again for your work. Now it's time for me to convince my team to move forward with Cinch

Nov 30, 2011 at 1:48 PM

I will put it on the list, its one I need to try at home and make sure its ok. But if you look at the Cinch V2 WPF demo, I do add new Views at runtime using context menu, where you can add new about/Image views dynamically using right click. So it does appear to work, so there is something strange in your requirements I think that is different from what I am doing.