开发者

mvvm command setup styles

Commands here would be some variation of Josh Smith's RelayCommand (I call mine a VmCommand), and the question is abouthow your ViewModel creates them. I'm aware of two basic techniques, the first being that you set up all command properties inside the constructor, the seco开发者_StackOverflow社区nd being you instantiate the command lazily inside a property getter.

I prefer the latter as I feel it lets me keep my code more organized, as I will typically wrap all behavior related to a given feature in it's own region, as shown below for a SaveCommand.

How do you like to setup your commands?

Cheers,

Berryl

    #region Saving

    public ICommand SaveCommand
    {
        get
        {
            return _saveCommand ?? (_saveCommand = new VmCommand
            {
                CanExecuteDelegate = x => CanSave(),
                ExecuteDelegate = x => Save()
            });
        }
    }
    private ICommand _saveCommand;

    private bool CanSave() { return IsDirty; }

    public void Save()
    {
        _facade.Save();
    }

    #endregion


That's about what I do... however, when IsDirty changes, don't expect your buttons to automatically enable themselves.

You'll need to tell your command to fire CanExecuteChanged and call CommandManager.InvalidateRequerySuggested, otherwise you'll find your buttons don't always respond to changes to the "can execute" status of the command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜