0

我已经看到问题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);
        }
    }
}
4

1 回答 1

1

怎么样:

RetrieveRecord = new ReactiveAsyncCommand(
    this.WhenAny(x => x.UniqueId, x => x.Value.Length == 7));
于 2011-10-21T19:46:50.277 回答