ReactiveUI:CanExecute具有非集合属性

时间:2011-10-21 19:42:22

标签: reactiveui reactivevalidatedobject

我见过问题ReactiveUI: Using CanExecute with a ReactiveCommand,但我的问题是我有一个字符串属性,UniqueID,我希望它只在长度等于7时执行。我似乎无法想出一个不会使程序崩溃的观察者。这样做的正确简单方法是什么?

public class MainViewModel : ReactiveValidatedObject
{
    public MainViewModel()
    {
        RetrieveRecord = new ReactiveAsyncCommand(/* what goes here for CanExecute */);
        RetrieveRecord.Subscriber(x => Record = new Record(UniqueId));

        // or do we use the method RetrieveRecord.CanExecute()?
        // the next line crashes the app

        RetrieveRecord.CanExecute(UniqueId.Length == 7);
    }

    public ReactiveAsyncCommand RetrieveRecord { get; private set; }

    string _uniqueId;
    public string UniqueId
    {
        get { return _uniqueId; }
        set
        {
            _clientId = value;
            this.RaisePropertyChanged(x => x.UniqueId);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

怎么样:

RetrieveRecord = new ReactiveAsyncCommand(
    this.WhenAny(x => x.UniqueId, x => x.Value.Length == 7));