C# 带有CanExecute的组合条件的ReactiveCommand

C# 带有CanExecute的组合条件的ReactiveCommand,c#,system.reactive,reactiveui,C#,System.reactive,Reactiveui,我刚刚开始使用ReactiveUI。我有以下课程: public class MainViewModel : ReactiveObject, IRoutableViewModel { private string shareText; public ReactiveCollection<SharingAccountViewModel> SelectedAccounts { get; private set; } public string ShareText

我刚刚开始使用ReactiveUI。我有以下课程:

public class MainViewModel : ReactiveObject, IRoutableViewModel 
{
   private string shareText;

   public ReactiveCollection<SharingAccountViewModel> SelectedAccounts { get; private set; }

   public string ShareText 
   { 
      get { return shareText; }
      set
      {
          this.RaiseAndSetIfChanged(ref shareText, value);
      }
   }

   public IReactiveCommand ShareCommand { get; private set; }
}
但是,如果我只检查ShareText属性,它可以正常工作:

  ShareCommand = new ReactiveCommand(this.WhenAny(viewModel => viewModel.ShareText,
            (x) => !String.IsNullOrEmpty(x.Value)));
我看了看问题的答案

基于此,我尝试了以下方法:

var accountsSelected = SelectedAccounts.CollectionCountChanged.Select(count => count > 0);
ShareCommand = new ReactiveCommand(accountsSelected);
这适用于我的执行条件的第二部分,因为一旦在集合中添加或删除项,连接到命令的按钮就会正确启用或禁用

我的问题是,我现在如何结合检查ShareText属性是否为null或空

我不能再使用this.WhenAny(..)方法,因为accountsSelected变量不是属性


谢谢

我相信有不同的方法可以达到预期的效果,这只是一种方法

var canExecute = this.ObservableForProperty(v => v.ShareText)
                         .Select(_ => Unit.Default)
                  .Merge(SelectedAccounts.CollectionCountChanged
                                     .Select(_ => Unit.Default))
                  .Select(_ => !String.IsNullOrEmpty(ShareText)
                                    && SelectedAccounts.Any())
                  .StartWith(false);

ShareCommand = new ReactiveCommand(canExecute);

我相信有不同的方法可以达到预期的效果,这只是一种方法

var canExecute = this.ObservableForProperty(v => v.ShareText)
                         .Select(_ => Unit.Default)
                  .Merge(SelectedAccounts.CollectionCountChanged
                                     .Select(_ => Unit.Default))
                  .Select(_ => !String.IsNullOrEmpty(ShareText)
                                    && SelectedAccounts.Any())
                  .StartWith(false);

ShareCommand = new ReactiveCommand(canExecute);

使用WhenAny和
IObservable
有点棘手。我会这样做:

var canShare = Observable.CombineLatest(
    this.WhenAnyObservable(x => x.SelectedAccounts.CollectionCountChanged),
    this.WhenAny(x => x.ShareText, x => x.Value),
    (count, text) => count > 0 && !String.IsNullOrWhitespace(text));

在此处不可见时的优点是,如果您决定重新分配所选帐户(即
SelectedAccounts=new ReactiveCollection(…);
,上面的语句仍然有效,而上面的语句仍然在听旧的集合。

使用WhenAny和
IObservable
有点棘手。我将这样做:

var canShare = Observable.CombineLatest(
    this.WhenAnyObservable(x => x.SelectedAccounts.CollectionCountChanged),
    this.WhenAny(x => x.ShareText, x => x.Value),
    (count, text) => count > 0 && !String.IsNullOrWhitespace(text));

在此处不可见时的优点是,如果您决定重新分配所选帐户(即
SelectedAccounts=new ReactiveCollection(…)
,上面的陈述仍然有效,而上面的陈述仍然是旧的集合。

谢谢你,保罗。我喜欢你的解决方案,因为它更紧凑,更容易理解。试着让我的头脑了解Rx…爱你在ReactiveUI上的工作谢谢你,保罗。我喜欢你的解决方案,因为它更复杂约定,对我来说稍微容易理解。试着让我的头脑了解Rx…喜欢你在ReactiveUI上的工作顺便说一句