ReactiveUI RaiseAndSetIfChanged 是否为 List<T> 触发?添加、删除、修改? [英] Does ReactiveUI RaiseAndSetIfChanged fire for List&lt;T&gt; Add, Delete, Modify?

查看:28
本文介绍了ReactiveUI RaiseAndSetIfChanged 是否为 List<T> 触发?添加、删除、修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这会不会起火

私有列表_TweetTiles;公共列表微博{得到 { 返回 _TweetTiles;}设置 { this.RaiseAndSetIfChanged(ref _TweetTiles, value);}}

当我这样做时:

TweetTiles.Add(new TweetTileViewModel(...));或者TweetTiles.Remove(new TweetTileViewModel(...));

我怀疑不是,但我不确定我应该如何得到这种行为.

我有一个在后台任务上运行的函数,它返回推文.每次我收到一条新推文时,我都会将其转换为 TweetTileViewModel 并希望它显示在我的 UI 列表框中.

解决方案

这会起火吗?

不,这只会在您设置列表时触发:

TweetTiles = new List();

<块引用>

我有一个在后台任务上运行的函数,它返回推文.每次我收到一条新推文时,我都会将其转换为 TweetTileViewModel 并希望它显示在我的 UI 列表框中.

ReactiveUI 通过 ReactiveList 类内置了对此的支持.这是您想要设置的方式 - 首先,声明一些属性:

ReactiveList推文 { 得到;保护集;}IReactiveDerivedListTweetViewModels { 获取;保护集;}ReactiveCommand LoadTweets { get;保护集;}

然后,在构造函数中:

//注意:仅在 UI 线程上更改此列表!Tweets = new ReactiveList();//现在,当您添加和删除项目时,TweetViewModels 将跟随"推文TweetViewModels = Tweets.CreateDerivedCollection(推特=>新 TweetTileViewModel(tweet));LoadTweets = new ReactiveCommand();var newTweets = LoadTweets.RegisterAsyncTask(async _ => {//加载最新的推文返回 LoadTweetsAsync();});newTweets.Subscribe(newTweets => {//添加我们下载的新推文.这是由//ReactiveCommand 在 UI 线程上Tweets.AddRange(newTweets);});

更新: 修正了类型错误.要使用仅发送回调的方法,请使用 AsyncSubject:

public IObservable>GetTweetsObservable(这个 TwitterClient 这个){var ret = new AsyncSubject>();尝试 {This.GetTweetsWithCallback(tweets => {ret.OnNext(tweets);ret.OnCompleted();});} 捕捉(异常前){ret.OnError(ex);}返回 ret;}

现在,您可以将 RegisterAsyncTask 更改为 RegisterAsync 就可以了!

Will this fire

private List<TweetTileViewModel> _TweetTiles;
    public List<TweetTileViewModel> TweetTiles
    {
        get { return _TweetTiles; }
        set { this.RaiseAndSetIfChanged(ref _TweetTiles, value); }
    }

When I do:

TweetTiles.Add(new TweetTileViewModel(...)); or
TweetTiles.Remove(new TweetTileViewModel(...));

etc.

I suspect not, but am not sure how I should get this behavior.

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.

解决方案

Will this fire?

Nope, this will only fire when you set the list:

TweetTiles = new List<TweetTileViewModel>();

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.

ReactiveUI has built-in support for this, via the ReactiveList class. Here's how you want to set it up - first, declare some properties:

ReactiveList<Tweet> Tweets { get; protected set; }
IReactiveDerivedList<TweetTileViewModel> TweetViewModels { get; protected set; }

ReactiveCommand LoadTweets { get; protected set; }

Then, in the constructor:

// Note: Only change this list on the UI thread!
Tweets = new ReactiveList<Tweet>();

// Now, TweetViewModels will "follow around" Tweets as you add and remove items
TweetViewModels = Tweets.CreateDerivedCollection(
    tweet => new TweetTileViewModel(tweet));

LoadTweets = new ReactiveCommand();

var newTweets = LoadTweets.RegisterAsyncTask(async _ => {
    // Loads the latest tweets
    return LoadTweetsAsync();
});

newTweets.Subscribe(newTweets => {
    // Add in the new tweets we downloaded. This is guaranteed by
    // ReactiveCommand to be on the UI thread
    Tweets.AddRange(newTweets);
});

Update: Fixed type error. To use a method that only sends a callback, use an AsyncSubject:

public IObservable<List<Tweet>> GetTweetsObservable(this TwitterClient This)
{
    var ret = new AsyncSubject<List<Tweet>>();

    try {
        This.GetTweetsWithCallback(tweets => {
            ret.OnNext(tweets);
            ret.OnCompleted();
        });
    } catch (Exception ex) {
        ret.OnError(ex);
    }

    return ret;
}

Now, you can change RegisterAsyncTask to just RegisterAsync and you're good to go!

这篇关于ReactiveUI RaiseAndSetIfChanged 是否为 List<T> 触发?添加、删除、修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆