ReactiveUI - WhenActivated 和输出属性
ReactiveUI - WhenActivated and Output Properties
我正在使用 ReactiveUI and I'm trying to change all my ViewModels' rules with WhenActivated,但我无法理解如何让它与输出属性一起正常工作。
模式类似于
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);
所以我认为统一这些的唯一方法是:
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);
});
}
}
这会按预期工作还是有其他更好的方法?
在这种情况下,不需要处理 _Obj,因为 Something 与 _Obj 具有相同的作用域(它们都包含在 MyClass 中)。但是,如果您要将 _Obj 基于服务中包含的可观察对象(通常比视图模型具有更长的生命周期),那么您需要处理它。
// _Obj should be disposed of, in this case.
_Obj = someService
.SomePipeline
.ToProperty(this, x => x.Obj)
旁注:如果要分配给一个值,则不需要使用 out 参数。下面等价于上面的:
someService
.SomePipeline
.ToProperty(this, x => x.Obj, out _Obj)
所以对于 someService,你 可以 将 _Obj 放在 WhenActivated 中,但它
creates sticky race conditions whereby the OAPH may be null when it's
corresponding property (Obj) is accessed too early.
此信息以及上面的引用直接摘自 Kent Boogaart 的书 "You, I, and ReactiveUI."
他很友好地为上述场景提供了将激活变成管道的解决方案。您可以像这样使用 these extensions(第一个用于视图模型,第二个用于视图):
_Obj = this
.GetIsActivated()
.Select(isActivated => isActivated ? someService.SomePipeline : Observable.Return(null))
.Switch()
.ToProperty(this, x => x.Obj);
我正在使用 ReactiveUI and I'm trying to change all my ViewModels' rules with WhenActivated,但我无法理解如何让它与输出属性一起正常工作。
模式类似于
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);
所以我认为统一这些的唯一方法是:
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);
});
}
}
这会按预期工作还是有其他更好的方法?
在这种情况下,不需要处理 _Obj,因为 Something 与 _Obj 具有相同的作用域(它们都包含在 MyClass 中)。但是,如果您要将 _Obj 基于服务中包含的可观察对象(通常比视图模型具有更长的生命周期),那么您需要处理它。
// _Obj should be disposed of, in this case.
_Obj = someService
.SomePipeline
.ToProperty(this, x => x.Obj)
旁注:如果要分配给一个值,则不需要使用 out 参数。下面等价于上面的:
someService
.SomePipeline
.ToProperty(this, x => x.Obj, out _Obj)
所以对于 someService,你 可以 将 _Obj 放在 WhenActivated 中,但它
creates sticky race conditions whereby the OAPH may be null when it's corresponding property (Obj) is accessed too early.
此信息以及上面的引用直接摘自 Kent Boogaart 的书 "You, I, and ReactiveUI."
他很友好地为上述场景提供了将激活变成管道的解决方案。您可以像这样使用 these extensions(第一个用于视图模型,第二个用于视图):
_Obj = this
.GetIsActivated()
.Select(isActivated => isActivated ? someService.SomePipeline : Observable.Return(null))
.Switch()
.ToProperty(this, x => x.Obj);