【问题标题】:ReactiveUI observing Collection with WhenAny and assigning boolean PropertyHelperReactiveUI 使用 WhenAny 观察 Collection 并分配 boolean PropertyHelper
【发布时间】:2019-07-24 23:05:41
【问题描述】:

我有一个 ObservableCollection,其中包含我想要观察的字符串。当第一个索引发生变化时,我想更新 ObservableAsPropertyHelper 布尔变量,如果该索引处的字符串不为空且不为空,则将其设置为 true。或者,将 Collection 转换为包含 bool 的第二个 ObservableCollection 的方法也可以。
这是我尝试过的:

public ObservableCollection<string> Difficulties { get; set; }
public extern bool Easy { [ObservableAsProperty] get; }

public Song()
{
    this.WhenAny(x => x.Easy, x => x.GetValue()[0] != null && x.GetValue()[0}]!= "").ToPropertyEx(this, x => x.Easy);
}

我尝试了上面的代码,因为它适用于类似的示例:

[Reactive] public string Title { get; set; }
public extern bool Enabled { [ObservableAsProperty] get; }

public Song()
{
   this.WhenAny(x => x.Title, x => x.GetValue() != null && x.GetValue() != "").ToPropertyEx(this, x => x.Enabled);
}

我对使用 Linq 表达式很陌生,所以解决方案可能很简单(比我尝试过的东西容易得多 -_-)
顺便说一句,我在 .net core 3.0 上使用 ReactiveUi.WPF 和 ReactiveUi.Fody。

【问题讨论】:

    标签: c# list linq lambda reactiveui


    【解决方案1】:

    WhenAnyValue 扩展方法通常用于您想观察视图模型属性变化的情况,并且非常适合这样做。它不是为使用可变集合而设计的,例如ObservableCollectionReadOnlyObservableCollection。对于响应式集合,请使用 DynamicData 库,也请参阅相关的 documentation page。最新的 ReactiveUI 版本依赖于 DynamicData,因此您无需安装任何额外的包。

    TLDR;

    1. 如果您有T 类型的属性,则使用WhenAnyValue

    2. 如果您有ObservableCollection&lt;T&gt; 类型的属性,请使用ToObservableChangeSet

    [ObservableAsProperty] 
    public bool Easy { get; }
    
    public ObservableCollection<string> Difficulties { get; }
    
    public Song()
    {
        Difficulties = new ObservableCollection<string>();
    
        // Observe any changes in the observable collection.
        // Note that the property has no public setters, so we 
        // assume the collection is mutated by using the Add(), 
        // Delete(), Clear() and other similar methods.
        this.Difficulties
            // Convert the collection to a stream of chunks,
            // so we have IObservable<IChangeSet<TKey, TValue>>
            // type also known as the DynamicData monad.
            .ToObservableChangeSet(x => x)
            // Each time the collection changes, we get
            // all updated items at once.
            .ToCollection()
            // If the collection isn't empty, we access the
            // first element and check if it is an empty string.
            .Select(items => 
                items.Any() &&
                !string.IsNullOrWhiteSpace(items.First()))
            // Then, we convert the boolean value to the
            // property. When the first string in the
            // collection isn't empty, Easy will be set
            // to True, otherwise to False.
            .ToPropertyEx(this, x => x.Easy);
    }
    

    注意,如果您使用的是不可变数据集,例如有点像这样:

    [Reactive] public IEnumerable<string> Difficulties { get; set; }
    

    并且该数据集仅通过其公共设置器更新,那么您应该在此处使用WhenAnyValue 扩展方法,因为声明为IEnumerable&lt;string&gt; 的集合是不可观察的:

    this.WhenAnyValue(x => x.Difficulties)
        .Select(items => 
            items.Any() &&
            !string.IsNullOrWhiteSpace(items.First()))
        .ToPropertyEx(this, x => x.Easy);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2020-01-06
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多