Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Relay Command

$
0
0

I had a question, I tried to make a slight modification to implementing RelayCommand (http://msdn.microsoft.com/en-us/magazine/dd419663.aspx and rather than implement as Josh Smith did (via a public property and then returning a new instance of a RelayCommand in the getter), I instead tried to short-circuit it by creating the public properties and then setting the commands to a new instance of RelayCommand in the VMs constructor (my only point was to write more concise code – the getter implementation seemed wordy to me).

 

It seemed to me that this would produce about the same result. However when I run it, the commands don’t fire the associated method (though I do see the command being set during the VMs constructor).

 

I then cut back to the Josh Smith style and everything works OK. Could you tell me what I’m missing and what prevents me from writing code this way?

 

Thanks much in advance for any help.

 

Snippet of code:

 

   classvmMain

   {

       publicRelayCommand CancelActivityCommand;

       publicRelayCommand CancelAllCommand;

       publicRelayCommand SaveActivityCommand;

       publicRelayCommand SaveAllCommand;

       publicRelayCommand PrintActivityCommand;

       publicRelayCommand ExitAppCommand;

       publicRelayCommand NavigateBackCommand;

 

       public vmMain()

       {

           CancelActivityCommand =

               newRelayCommand (param =>this.CancelActivity(),

                    param =>this.hasActivity);

           CancelAllCommand =

               newRelayCommand (param =>this.CancelAll(),

                   param => this.hasModels);

           SaveActivityCommand =

               newRelayCommand (param =>this.SaveActivity(),

                   param => this.hasActivity);

           SaveAllCommand =

               newRelayCommand (param =>this.SaveAll(),

                   param => this.hasModels);

           PrintActivityCommand =

               newRelayCommand (param =>this.PrintActivity(),

                    param =>this.hasActivity);

           ExitAppCommand =

               newRelayCommand (param =>this.ExitApp());

           NavigateBackCommand =

               newRelayCommand (param =>this.NavigateBack());

       }

}


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>