我想知道绑定到ReactiveCommand的IsExecuting
的推荐方法。
问题是最初的命令执行(从构造函数的末尾开始)没有使用IsLoading
作为绑定更新WPF控件,尽管后续的调用按预期工作。
更新2添加测试绑定代码
这显示了当IsLoading
为真时装饰器的内容。
<ac:AdornedControl IsAdornerVisible="{Binding IsLoading}">
<ac:AdornedControl.AdornerContent>
<controls1:LoadingAdornerContent/>
</ac:AdornedControl.AdornerContent>
<fluent:ComboBox
ItemsSource="{Binding Content, Mode=OneWay}"
DisplayMemberPath="Name"
SelectedValuePath="ContentId"
SelectedValue="{Binding SelectedContentId}"
IsSynchronizedWithCurrentItem="True"
/>
</ac:AdornedControl>
更新
我发现了这个:https://github.com/reactiveui/rxui-design-guidelines
我想我应该能做这样的事:
this._isLoading = this.WhenAnyValue(x => x.LoadCommand.IsExecuting)
.ToProperty(this, x => x.IsLoading);
但是它给出了编译错误:
不能从用法中推断方法'ReactiveUI.OAPHCreationHelperMixin.ToProperty< TObj、TRet>(System.IObservable< TRet>、TObj、System.Linq.Expressions.Expression< System.Func< TObj、TRet>>、TRet、System.Reactive.Concurrency.IScheduler)的类型参数。尝试显式指定类型参数。
我也试过:
this._isLoading = this.WhenAnyValue(x => x.LoadCommand.IsExecuting)
.ToProperty<TheViewModel, bool>(this, x => x.IsLoading);
但是获取编译错误:
'System.IObservable< System.IObservable< bool >>‘不包含'ToProperty’的定义,最佳的扩展方法重载ToProperty TObj,TRet>(System.IObservable< TRet>,TObj,System.Linq.Expressions.Expression< System.Func< TObj,TRet>>,TRet,System.Reactive.Concurrency.IScheduler)‘有一些无效的参数。
和
实例参数:无法从“System.IObservable>”转换为“System.IObservable”
原件在下面
文章末尾列出的代码通过访问IsLoading
属性为初始绑定工作,听起来就像开始订阅一样。但从进一步的阅读来看,我似乎应该使用WhenAny
,而且我似乎无法弄清楚在我眼前的是什么:
增加:
this.WhenAnyValue(x => x.LoadCommand.IsExecuting);
同样有效,但是有没有更好的方法呢?
我正在考虑删除ObservableAsPropertyHelper
,因为它似乎对我并没有多大帮助,并使IsLoading
成为一个正常的属性,比如:
private bool _isLoading;
public bool IsLoading
{
get { return _isLoading; }
set { this.RaiseAndSetIfChanged(ref _isLoading, value); }
}
并执行如下操作,但它没有编译,因为它试图将一个IObservable< bool>
分配给bool:
this.WhenAnyValue(x => x.LoadCommand.IsExecuting)
.Subscribe(x => IsLoading = x);
当前代码:
private readonly ObservableAsPropertyHelper<bool> _isLoading;
public bool IsLoading
{
get { return _isLoading.Value; }
}
LoadCommand = ReactiveCommand.CreateAsyncTask(async _ =>
{
//go do command stuff like fetch data from a database
}
LoadCommand.IsExecuting.ToProperty(this, x => x.IsLoading, out _isLoading);
//works if I have this line
var startSubscription = IsLoading;
LoadCommand.ExecuteAsyncTask();
发布于 2014-09-02 19:11:46
我想我应该能做这样的事:
您已经有了正确的想法,但是语法有点错误,请尝试:
this.LoadCommand.IsExecuting
.ToProperty(this, x => x.IsLoading, out _isLoading);
如果您要使用可以更改的对象(例如,您有一个长表达式)来执行此操作,则有一个名为WhenAnyObservable
的特殊方法,您可以使用它来代替WhenAnyValue
this.WhenAnyObservable(x => x.SomeObjectThatMightBeReplaced.IsExecuting)
.ToProperty(this, x => x.IsLoading, out _isLoading);
发布于 2014-09-02 13:34:13
我以前也遇到过这种情况,我认为你们正在经历的是谎言,这里。
ToProperty / OAPH更改
它很懒,所以您必须订阅它(如果使用OAPH,请从属性中请求一个值),这样它才能工作。这就是为什么你注意到你的var startSubscription = IsLoading;
“修复”了这个问题。
知道这一点使我更容易确定这是否是一个问题,或者只是在我的单元测试中记住一些东西,因为我知道在我的应用程序中这些属性将被绑定并因此被订阅,使得它在实践中变得毫无意义。你知道,整个“树倒在森林里,没有人在那里听”的想法。
我认为你应该坚持你所拥有的ToProperty
,这似乎是走IMHO的路。
https://stackoverflow.com/questions/25590339
复制相似问题