Question

Let's say you have a view-model that controls a media player. It has to offer standard control features like Star, Stop, Pause and Resume.

I'm thinking of two ways to design it in the view-model

1st Way : Every action has its own command. - StartCommand - StopCommand - PauseCommand - ResumeCommand

Every button in the view will be bound to it's related command.

2nd way : One single command with different commandparameters. PerformActionCommand and an enumeration that would look like

enum ActionEnum
{
  start, 
  stop,
  pause,
  resume
}

Which way do you think is preferable and for what reasons ?

Was it helpful?

Solution

In this case using separate commands is better. The commands are not very alike, so if you have one command handler, you will have to use a large switch statement inside and probably call separate methods.

Another reason is that with different commands you easily can have different conditions when those commands are enabled or not - for example Resume is enabled only when the player is paused.

OTHER TIPS

In addition to what Jogy writes I would also go for separate commands. In this way you can also use the built-in wpf media commands.

This has some potential advantages:

  • you don't need to write a class implementing ICommand for each command because it is already inside WPF

  • Each media command is actually a RoutedUICommand, which means it has the text propety that you can use as content for you button.

  • You may take advantage of already built-in input gestures with some commands.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top