Can someone help me translating DelegateCommand to ReactiveCommand

119 views
Skip to first unread message

Martin H. Andersen

unread,
May 8, 2013, 9:05:12 AM5/8/13
to reacti...@googlegroups.com

I have this

 public ICommand ClosingCommand
 {
     get { return _closingCommand ?? (_closingCommand = new DelegateCommand(ExecuteClosing, CanExecuteClosing)); }
 }

  private void ExecuteClosing()
        {
            Exit();
        }


        private bool CanExecuteClosing()
        {

            var result = MessageBoxResult.None;
            if (IsDirty)
            {
                const string confirmText = "There are unsaved changes. Do you want to save your changes before exit?";
                const string header = "Save changes?";

                const MessageBoxButton button = MessageBoxButton.YesNoCancel;
                const MessageBoxImage icon = MessageBoxImage.Warning;
                result = MessageBox.Show(confirmText, header, button, icon);

                if (result == MessageBoxResult.Yes)
                    Commands.SaveCommand.Execute(null);
            }
            return result != MessageBoxResult.Cancel;

And have tried this:

CloseCommand = new ReactiveCommand(this.WhenAny(x => x.CanExecuteClosing, x => x.Value));
CloseCommand.Subscribe(x => ExecuteClosing());

but CanExecuteClosing is never executed

Paul Betts

unread,
May 8, 2013, 1:25:17 PM5/8/13
to reacti...@googlegroups.com
Hi Martin,

I'm not exactly sure what you're trying to do here in the ReactiveCommand
constructor. What is the type of CanExecuteClosing?

It might make more sense to do:

CloseCommand = new ReactiveCommand();

CloseCommand
.Where(_ => AskUserIfWeShouldClose() == true)
.Subscribe(_ => Exit());

--
Paul Betts <pa...@paulbetts.org>
> --
> You received this message because you are subscribed to the Google Groups "ReactiveUI mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to reactivexaml...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

IanYates

unread,
May 8, 2013, 7:22:05 PM5/8/13
to ReactiveUI mailing list
Hi,

The WhenAny() method is used to monitor properties / observables for
changes - your CanExecuteClosing call is a method. I'm a little
surprised it's actually compiling without the brackets after
CanExecuteClosing in the WhenAny() call.

The guard condition that you can give to the ReactiveCommand
constructor shouldn't include blocking calls, especially those that
prompt the user. The condition could be evaluated *many* times and
you don't want that prompt being thrown up to the user over and over.

Paul's suggestion makes sense to me. It will also have the (probably
desired) effect of leaving your command available for execution, then
the Where() clause will ensure your method is called and block until
there's an answer from the user. If the user answers in the
affirmative then Where clause will let the subscription run.


On May 8, 11:05 pm, "Martin H. Andersen"

Martin H. Andersen

unread,
May 11, 2013, 11:13:37 AM5/11/13
to reacti...@googlegroups.com
Thanks to both of you

to handle closing of my application.

But it's not working with the code that Paul recommended. This is the code that is executed on window.closing

private static void Window_Closing(object sender, CancelEventArgs e)
        {
            ICommand closing = GetClosing(sender as Window);
            if (closing == null) return;

            if (closing.CanExecute(null))
                closing.Execute(null);
            else
            {
                ICommand cancelClosing = GetCancelClosing(sender as Window);
                if (cancelClosing != null)
                    cancelClosing.Execute(null);

                e.Cancel = true;
            }
        }
 
closing.CanExecute is always true

--
You received this message because you are subscribed to a topic in the Google Groups "ReactiveUI mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivexaml/u5thUjMslBw/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to reactivexaml...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.





--

Med venlig hilsen / Best regards

 

Martin H.  Andersen

System developer

Reply all
Reply to author
Forward
0 new messages