Thursday, December 16, 2010

Commands and Command Parameters

Have you ever tried to use DelegateCommand<T> with enums being your payload?

The quick answer is that you can't. DelegateCommand<T> requires that T being a struct or class type (that is everything except enum!). So how do you get around this (god knows we hate "work-arounds").

I found that the best way to use enum-like functionality is to convert the enum into a class of constant strings. A very good example is a menu with enum type locations (eg. "Home", "Settings", "Update", etc.). By converting this enum into a class, we have:


public class LocationNames
{
    public const string Home = "Home";
    public const string Update = "Update";
    public const string Setting = "Setting";
}

So now our enum DelegateCommand<T> can be DelegateCommand<string> instead.

1 comment:

  1. Or as a better option, keep the enum class as is and pass the payload as a nullable enum. (I.e. Then in your command handler either check that the payload isn't null or use ".HasValue" before doing anything with the payload.

    ReplyDelete