Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Will this fire?</p> </blockquote> <p>Nope, this will only fire when you <em>set</em> the list:</p> <pre><code>TweetTiles = new List&lt;TweetTileViewModel&gt;(); </code></pre> <blockquote> <p>I have a function, running on a background Task, that is returning tweets. Each time I get a new tweet I convert it to a TweetTileViewModel and want it to show in my UI listbox.</p> </blockquote> <p>ReactiveUI has built-in support for this, via the <code>ReactiveList</code> class. Here's how you want to set it up - first, declare some properties:</p> <pre><code>ReactiveList&lt;Tweet&gt; Tweets { get; protected set; } IReactiveDerivedList&lt;TweetTileViewModel&gt; TweetViewModels { get; protected set; } ReactiveCommand LoadTweets { get; protected set; } </code></pre> <p>Then, in the constructor:</p> <pre><code>// Note: Only change this list on the UI thread! Tweets = new ReactiveList&lt;Tweet&gt;(); // Now, TweetViewModels will "follow around" Tweets as you add and remove items TweetViewModels = Tweets.CreateDerivedCollection( tweet =&gt; new TweetTileViewModel(tweet)); LoadTweets = new ReactiveCommand(); var newTweets = LoadTweets.RegisterAsyncTask(async _ =&gt; { // Loads the latest tweets return LoadTweetsAsync(); }); newTweets.Subscribe(newTweets =&gt; { // Add in the new tweets we downloaded. This is guaranteed by // ReactiveCommand to be on the UI thread Tweets.AddRange(newTweets); }); </code></pre> <p><strong>Update:</strong> Fixed type error. To use a method that only sends a callback, use an AsyncSubject:</p> <pre><code>public IObservable&lt;List&lt;Tweet&gt;&gt; GetTweetsObservable(this TwitterClient This) { var ret = new AsyncSubject&lt;List&lt;Tweet&gt;&gt;(); try { This.GetTweetsWithCallback(tweets =&gt; { ret.OnNext(tweets); ret.OnCompleted(); }); } catch (Exception ex) { ret.OnError(ex); } return ret; } </code></pre> <p>Now, you can change <code>RegisterAsyncTask</code> to just <code>RegisterAsync</code> and you're good to go!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload