ReactiveUI - WhenActivated 和输出属性 [英] ReactiveUI - WhenActivated and Output Properties

查看:48
本文介绍了ReactiveUI - WhenActivated 和输出属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ReactiveUI 并且我正在尝试使用WhenActivated 但我无法理解如何让它与输出一起工作良好属性.

I'm using ReactiveUI and I'm trying to change all my ViewModels' rules with WhenActivated but I'm unable understand how to have it working well in conjunction with output properties.

模式有点像

this.WhenActivated(
registerDisposable =>
{
    registerDisposable(this.Bind(…));
    registerDisposable(this.WhenAny(…));
    registerDisposable(this.WhenAnyValue(…));
});

并且输出属性看起来像

protected readonly ObservableAsPropertyHelper<object> _Obj;
public object Obj => _Obj.Value
_Obj = this.WhenAnyValue(x => x.Something).ToProperty(this, x => x.Obj, out _Obj);

所以我认为将这些统一起来的唯一方法是:

So the only way I see to unite these is:

class MyClass {

protected readonly ObservableAsPropertyHelper<object> _Obj;
public object Obj => _Obj.Value

public MyClass()
{
    _Obj = this.WhenAnyValue(x => x.Something).ToProperty(this, x => x.Obj, out _Obj);

    this.WhenActivated( registerDisposable =>
    {
        registerDisposable(_Obj);
    });
}

}

这会按预期工作还是有另一种更好的方法来做到这一点?

Will this work as expected or there is another better way to do it?

推荐答案

在这种情况下,_Obj 不需要处理,因为 Something 与 _Obj 具有相同的作用域(它们都是包含在 MyClass 中).但是,如果您要将 _Obj 建立在包含在服务中的 observable 的基础上(该服务的生命周期通常比视图模型更长),那么您需要处理它.

In this case, _Obj doesn't need to be disposed of because Something has the same scope as _Obj (they're both contained within MyClass). However, if you were to base _Obj off of an observable contained in a service - which commonly has a longer lifetime than a view model - then you'd need to dispose of it.

// _Obj should be disposed of, in this case.
_Obj = someService
    .SomePipeline
    .ToProperty(this, x => x.Obj)

旁注:如果您要分配一个值,则不需要使用 out 参数.以下等价于上述:

Side note: you don't need to use the out parameter if you're assigning to a value. The following is equivalent to the above:

someService
    .SomePipeline
    .ToProperty(this, x => x.Obj, out _Obj)

所以在 someService 的情况下,你可以把 _Obj 放在 WhenActivated 中,但是它

So in the case of someService, you could put _Obj inside WhenActivated but it

创建粘性竞争条件,从而 OAPH 可能为空过早访问相应的属性 (Obj).

creates sticky race conditions whereby the OAPH may be null when it's corresponding property (Obj) is accessed too early.

此信息以及上面的引用直接取自 Kent Boogaart 的书 "你、我和 ReactiveUI."

This info, as well as the quote above, is taken straight from Kent Boogaart's book, "You, I, and ReactiveUI."

他很友好地为上述场景提供了一个解决方案,将激活变成了管道.您可以使用 (第一个用于视图模型,第二个用于视图)像这样:

He was kind enough to provide a solution for the above scenario that turns activation into a pipeline. You can use these extensions (the first for view models and the second for views) like this:

_Obj = this
    .GetIsActivated()
    .Select(isActivated => isActivated ? someService.SomePipeline : Observable.Return(null))
    .Switch()
    .ToProperty(this, x => x.Obj);

这篇关于ReactiveUI - WhenActivated 和输出属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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