InterestedIn and CloseViewModel

Topics: Issues / bugs
Sep 3, 2012 at 7:51 AM

Hello

Question. Have DataWindow and a Button Binding to a command. In this command I want to save something to the database and close the viewmodel.

        private void OnSaveRecipeExecute()
        {
            _datacontext.SaveRecipe(RecipeName);            
            CloseViewModel(false);
        }

But other viewmodels are also interestedin this command. The otherviewmodel  don't get noticed about this command because the sending viewmodel is closed before.

What can I do to solve this?

Coordinator
Sep 3, 2012 at 7:39 PM

Yes, save the data in the Save method.

Inside your command, call the SaveViewModel() method or SaveAndCloseViewModel.

If you want, you can even use the IDispatcherService to put it to the back of the queue.

Sep 4, 2012 at 6:42 AM

Ok. Now I save the data in the Save method

        private void OnSaveRecipeExecute()
        {
            SaveAndCloseViewModel();
        }
        protected override bool Save()
        {
            _gateway.SaveRecipe(RecipeName);
            return base.Save();
        }

Now I have the same problem. The otherviewmodel  don't get noticed.

Or do I need the IDispatcherService anyway?

Coordinator
Sep 4, 2012 at 6:44 AM

Yes, because it is all executed on the same thread, the command will be finalized *after* the VM is closed.

Sep 4, 2012 at 8:05 AM

Ok. You use the word even so I thougt it is only an option.

Is there an example in the documentation of how to use IDispatcherService. Its not realy clear for me.

Coordinator
Sep 4, 2012 at 8:14 AM

No, we don't have docs for that. You can use it as:

var dispatcherService = GetService<IDispatcherService>();

dispatcherService.BeginInvoke(() => SaveAndCloseViewModel());

Sep 4, 2012 at 8:25 AM
Edited Sep 4, 2012 at 8:25 AM

Confused!?

In my other viewmodel I want to get noticed of the command SaveRecipe.

SaveRecipe = new Command(OnSaveRecipeExecute,null,"SaveRecipe");

with following it also don't get noticed.

var dispatcherService = GetService<IDispatcherService>();
dispatcherService.BeginInvoke(() => SaveAndCloseViewModel());

Coordinator
Sep 4, 2012 at 8:26 AM

ok, please send me a repro then.

Sep 4, 2012 at 8:39 AM
Edited Sep 4, 2012 at 8:40 AM

A little example here: 

public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        SaveRecipe = new Command(OnSaveRecipeExecute, null"SaveRecipe");
    }
 
    private void OnSaveRecipeExecute()
    {
        var dispatcherService = GetService<IDispatcherService>();
        dispatcherService.BeginInvoke(() => SaveAndCloseViewModel());
    }
 
    protected override bool Save()
    {
        _datacontext.SaveRecipe(RecipeName);
        return base.Save();
    }
 
    public Command SaveRecipe { getprivate set; }
 
}
 
[InterestedIn(typeof(MyViewModel))]
public class OtherViewModel : ViewModelBase
{
    public OtherViewModel()
    {
    }
 
    protected override void OnViewModelCommandExecuted(IViewModel viewModel, ICatelCommand command, Object commandParameter)
    {
        if (viewModel is OtherViewModel)
        {
            if (TagHelper.AreTagsEqual(command.Tag, "SaveRecipe"))
            {
                var vm = viewModel as OtherViewModel;
 
                // Do something
            }
        }
    }
}

what's wrong?

Coordinator
Sep 4, 2012 at 7:36 PM

Please try the latest version.

Sep 5, 2012 at 6:24 AM

Perfect. Thank you for your help and the bugfix