开发者

mvvm command setup styles

开发者 https://www.devze.com 2022-12-21 07:25 出处:网络
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 b

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

精彩评论

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

关注公众号