MVVM Observe VM Command
-
I am adapting a XAML Control to be MVVM compliant. Right now the XAML is actually not important, but there is one component that is giving me grief. The control is actually called from another view model on an event. Click here to add kind of thing. That command looks like this:
private void OnAddItem() { SubItemViewModel subVM = new SubItemViewModel(Key, null, false); IDisposable ss = Observable.FromEventPattern(subVM.Save, "ExecuteEnd").Take(1).Subscribe(x => { LoadList(); }); SubControl ctrl = new SubControl(); ctrl.DataContext = subVM; WorkspaceUserControlWindow w = new WorkspaceUserControlWindow(ctrl); w.Title = subVM.DisplayTitle ?? Key.Value + " - Add Item"; Observable.FromEventPattern(w, "Closed").Take(1).Subscribe(x => { if (ss != null) ss.Dispose(); }); w.Show(); }
This worked fine in a non-MVVM model using a delegate command, click events, etc... Now that I have migrated the VM over to MVVM I need to understand how to publish the Save command so that the calling event can listen for the ExecuteEnd and reload the data list as shown here:
IDisposable ss= Observable.FromEventPattern(subVM.Save, "ExecuteEnd").Take(1).Subscribe(x => { LoadList(); });
I have always used relay commands within my view models as I have never needed to observe a command like this. Thoughts? Cheers, --EA