ReactiveUI 如何更新现有值的 ReactiveList

ReactiveUI how to update ReactiveList of existing values

最近几个小时,我试图解决一个问题,即如何更新 ObservableCollection 中的现有值。我将在这里用一些例子解释我试图做什么。

正在 Xamarin.Froms ListView 中加载的 ObservableCollection。

ObservableCollection<SearchResult> _searchResults;
    public ObservableCollection<SearchResult> SearchResults
    {
        get { return _searchResults; }
        private set { this.RaiseAndSetIfChanged(ref _searchResults, value); }
    }

    ReactiveCommand<List<SearchResult>> _search;
    public ReactiveCommand<List<SearchResult>> Search
    {
        get { return _search; }
        private set { this.RaiseAndSetIfChanged(ref _search, value); }
    }

这是正在加载到 ObservableCollection 中的 ReactiveObject。第一次加载列表时,AvaText 和 PerText 为空,因为数据还没有。因此,我想遍历列表并使用 AvaText 和 PerText 的正确值更新每个 SearchObject 并获取该数据,我需要使用另一个 API 调用来更新它。最好的方法是什么?任何人都可以帮助我向正确的方向展示我吗?将不胜感激:)

        public class SearchResult : ReactiveObject
    {

        string _displayText;

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

        string _IdText;

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

        string _avaText;

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

        string _perText;

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

我查看了 ReactiveUI 的文档,我认为我应该用 .ToProperty 做些什么?

感谢您的阅读。

亲切的问候,

费尔南多

我刚注意到你的 Search ReactiveCommand。这可能也需要改变。查看下面我的 LoadStatsImp 函数,了解如何执行 SearchImp 函数。也许是这样的。

ObservableAsPropertyHelper<ObservableCollection<SearchResult>> _searchResults;
public ObservableCollection<SearchResult> SearchResults => _searchResults;

public ReactiveCommand<ObservableCollection<SearchResult>> Search { get; protected set; }

public TheClassTheseAreIn 
{       
   Search = ReactiveCommand.CreateAsyncTask(parameter => SearchImp(this.SearchCriteria));

   _searchResults = Search.ToProperty(this, x => x.SearchResults, new ObservableCollection<SearchResult>);
}

我相信可以从 SearchResult class 调用您对 AvaText 和 PerText 的更新。

我假设 AvaText 和 PerText 是只读的,在这种情况下,我将为 AvaText 和 PerText 使用 ObservableAsPropertyHelper 而不是您现在拥有的 RaiseAndSetIfChanged 实现。 SearchResult class 将查找其 IdText 以进行更改,然后它将调用 LoadStats ReactiveCommand。 LoadStats 使用 ToProperty 来更新 AvaText 和 PerText 。

大部分来自 example code at docs.reactiveui.net

public class SearchResult : ReactiveObject
{
   string _displayText;

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

    string _IdText;

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

   ObservableAsPropertyHelper<string> _avaText;
   public string AvaText => _avaText.Value;

   ObservableAsPropertyHelper<string> _perText;
   public string PerText => _perText.Value;

   public ReactiveCommand<StatsClass> LoadStats { get; protected set; }

   public ModelTile ()
   {
      LoadStats = ReactiveCommand.CreateAsyncTask(parameter => LoadStatsImp(this.IdText));

      LoadStats.ThrownExceptions
         .SubscribeOn(RxApp.MainThreadScheduler)
         .Subscribe(ex => UserError.Throw("Couldn't load stats", ex));

      _avaText= LoadStats.Select(stats => stats.Ava).ToProperty(this, x => x.AvaText, string.Empty);

      _perText= LoadStats.Select(stats => stats.Per).ToProperty(this, x => x.PerText, string.Empty);

      this.WhenAnyValue(x => x.IdText)
         .Where(x => !String.IsNullOrWhiteSpace(x))
         .InvokeCommand(LoadStats);
   }

   public static async Task<StatsClass> LoadStatsImp(string id)
   {
      var stats = await DownloadRss(id);

      System.Diagnostics.Debug.WriteLine($"number of stats: {stats}");

      return stats;
   }
}