【问题标题】:ReactiveUI: CanExecute with a non collection propertyReactiveUI:具有非集合属性的 CanExecute
【发布时间】:2011-10-21 19:42:21
【问题描述】:

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

【问题讨论】:

    标签: reactiveui reactivevalidatedobject


    【解决方案1】:

    怎么样:

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

    【讨论】:

    • 它不起作用。第二个参数上没有“长度”属性并且无法编译。我确实尝试了以下导致应用程序失败的方法: new ReactiveAsyncCommand(this.WhenAny(x => x.UniqueId, x => x.Value.Length == 7));
    • 第三个内部异常显示“对象引用未设置为对象的实例。”
    • 为 null 的对象是 MainViewModel,它为 null 的原因是由于 ctor 的新 ReactiveAsyncCommand 引发的错误。希望这是有道理的。
    • 之前的 2 个错误是“调用的目标已抛出异常。”
    • 试图粘贴堆栈跟踪,但它是巨大的。这里有一些片段。 [第一部分] ReactiveUI.WhenAnyMixin.c__DisplayClassd3.<WhenAny>b__a(IObserver1 主题)在 c:\\Users\\Administrator\\Dropbox\\ReactiveUI_External\\ReactiveUI\\VariadicTemplates.cs:line 40 [第二部分] ReactiveUI.Xaml .ReactiveAsyncCommand.commonCtor() in c:\\..\\ReactiveUI_External\\ReactiveUI.Xaml\\ReactiveAsyncCommand.cs:第 114 行 ReactiveUI.Xaml.ReactiveAsyncCommand..ctor() in c:\\..\\ReactiveUI_External \\ReactiveUI.Xaml\\ReactiveAsyncCommand.cs:第 37 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多