Tag Archives for Commands

One of my favorite thing in Silverlight is command. It makes the code look very simple and elegant. This is build on top of part 2 of the blog. As always, lets first look at the result.

The default initial screen

This is same as previous now. Now if you type the name differently and focus out(do not click on ‘Say Hello’) You will get the following screen

Now click on the ‘Say Hello’ button which will render the following screen.

You see that, when I click on say hello, the word command get added to the name text block and in the bottom label as well. What I want to do demonstrate here is that, it is very easy to do commanding. Lets look at the code. In this one, my view XAML changed a bit (MainPage.XAML) as follows

Nothing changed except the Button control. Now I have two additional attributes added. They are Command and CommandParameter. The Command in here says what command to execute when user click the button. The command parameter says what is the parameter for command. For command parameter, I am reading the Name text block and passing the text. Now lets look at the command implementation, it is implemented in the view model as follows

namespace simplelabel
{
using Jounce.Core.ViewModel;
using Jounce.Core.View;
using System.ComponentModel.Composition;
using Jounce.Core.Command;
using Jounce.Framework.Command;

We declared new command called ‘SayHelloCommand’ of type IActionCommand and it gets instantiated at the view model constructor with delegate on what is it supposed to do. In the delegate what we are doing is setting the Name value with current value in the text block and then add text ‘Command’ to distinguish between the command execution and focusing out of the text block. Since we are setting the Name field, both the text block and the bottom label, which are bound field take the new values.