Random programming things I'd want to remember

Saturday, March 22, 2014

Code snippet for MVVM command in a ViewModel

Since code snippets are not that hard to do, here is another one. This one gets all the plumbing for a command in a ViewModel, I used DelegateCommand class to abstract all the INotifyPropertyChanged functionality. See my other posts on the subject. Call this one in VisualStudio by its shortcut, dcmvvm.
<?xml version="1.0" encoding="utf-8"?>
  <CodeSnippet Format="1.0.0">
      <Title>Plumbing for an MVVM DelegateCommand</Title>
      <Description>Plumbing for an MVVM DelegateCommand for use in a ViewModel</Description>
          <ToolTip>Prefix for a private variable that holds command name.</ToolTip>
          <ToolTip>Prefix for a property Name that exposes the command to the View.</ToolTip>
      <Code Language="csharp">
        DelegateCommand _$cmd1$Command;
        public ICommand $cmd2$Command { get { return _$cmd1$Command; } }
        private void $cmd2$CommandAction(object obj)
            throw new NotImplementedException();
        //add the following line to ViewModel constructor or wherever else you initialize your commands:
        _$cmd1$Command = new DelegateCommand(this.$cmd2$CommandAction, this.Can$cmd2$);
        private bool Can$cmd2$(object obj)
            return true;

No comments: