2016-10-19 11 views
0

最後の数時間ObservableCollectionの既存の値をどのように更新できるかという問題を解決しようとしています。私は何をしようとしているのかここで説明します。ReactiveUI既存の値のReactiveListを更新する方法

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はデータがまだないので、nullです。だから私はリストを反復して、すべてのSearchObjectをAvaTextとPerTextの正しい値で更新し、別の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で何かをやるべきだと思いますか?

ありがとうございます。

敬具、

フェルナンド

答えて

1

私はちょうどあなたの検索ReactiveCommandに気づきました。それも変更する必要があります。 SearchImp関数の実行方法については、以下のLoadStatsImp関数を参照してください。多分何かのように。

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クラスのAvaTextとPerTextに呼び出すことができます。

私は、AvaTextとPerTextが読み取り専用であると仮定しています。その場合は、今持っているRaiseAndSetIfChanged実装ではなく、AvaTextとPerTextにObservableAsPropertyHelperを使用します。 SearchResultクラスは、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; 
    } 
} 
+0

から来たのは、あなたの答えをありがとう、私は私の質問は少し短く、明確ではなかったことに気づきました。だから私はそれを編集して、あなたは私がこれにどのように接近すべきかについて、他の解決法や他の考え方を持っているかもしれません。 –

+0

SearchResultクラスで動作するように少し修正しました。それがうまくいかず、どんなエラーが出ているのか教えてください。 –

+0

それは動作します!どうもありがとうございました。私が今得ている唯一のものは、Refit拡張エラーです。あまりにも多くのリクエストをしなければならないので、 "400 Bad Request"というエラーメッセージが表示されます。もう一度あなたのすべての助けに感謝します:) –

関連する問題