Why my Commands did not work in Jounce

If you haven’t used commands in Silverlight, you have to try it. It is another beautiful way to move code from code behind to view model. If want to know how to use Commands, have a look at Jounce Codeplex or have a look at my blog.

Now lets go the problem I faced.  In XAML the Command and CommandParameter was defined properly. In the ViewModel both the commands are declared public and are initialized in the ViewModel constructor. Also both the execute methods are are declared and defined properly.  Now I was told, no compilation error and when you run, the command method never hits. After 30 minutes of debugging (I know it took some time, when you are not looking for the obvious), I found out that the command declarations were missing the getter and setter. Right after adding the getter and setter, it worked like a charm. So it is important to note that, if you are exposing something from ViewModel to view, make sure Jounce can get to it by declaring it as property.

Just in case you run into any problems, here are the things you need to watch out for using Commands

1. Make sure Command and CommandParameters are defined in the XAML and they bound to appropriate fields in ViewModel.

2. Make sure the command variables are declared ‘public’ and have getters and private setter.  ‘{ get; private set; }’

3. In the ViewModel constructor, you instantiate the command with execute method.

ButtonCommand = new ActionCommand<string>(CommandMethodToExecute)

4. Make sure the method signature correct. (In this case, if it is wrong, it will give compilation error).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s