- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 .NET 4.5 中使用 RXUI 6 和 WPF。
当它绑定(bind)到的 ViewModel 属性由 ObservableAsPropertyHelper
支持时,我一直无法获得提供给我的 View 的初始值。 .
根据documentation :
ToProperty / OAPH changes
ObservableAsPropertyHelper no longer is itself an IObservable, use WhenAny to observe it.
ObservableAsPropertyHelper now lazily Subscribes to the source only when the Value is read for the first time. This significantly
improves performance and memory usage, but at the cost of some "Why
doesn't my test work??" confusion. If you find that your ToProperty
"isn't working", this may be why.
我看了this question这似乎解决了我同样的问题,但提供的答案适用于测试和 ReactiveCommand
.我想不出最干净的方法来使它在我的情况下与任何 IObservable<>
一起工作不一定是 ReactiveCommand
(下面过于简单化)。
示例 View 模型:
public class ViewModel : ReactiveObject
{
private readonly ObservableAsPropertyHelper<string> _message;
public ViewModel()
{
var someObservable = Observable.Return("Hello");
_message = someObservable
.ToProperty(this, t => t.Message);
}
public string Message
{
get
{
return _message.Value;
}
}
}
示例 View 代码隐藏:
public partial class View : UserControl, IViewFor<ViewModel>
{
public View()
{
InitializeComponent();
this.WhenAnyValue(t => t.ViewModel.Message)
.BindTo(this, t => t.MessageTextBlock.Text);
}
// ... IViewFor Stuff....
}
所以现在,消息文本框将不包含初始值。但是,如果在我的 ViewModel 中,我要将行添加到构造函数中:
this.WhenAnyValue(t => t.Message).Subscribe(s => {});
它现在将触发 TextBlock,因为现在有一个订阅。所以我猜 .BindTo()
方法实际上从来没有算作订阅?还是懒惰之上的懒惰?这个空订阅是否抵消了它懒惰带来的性能优势?或者我不应该使用 .BindTo()
只需使用 .Subscribe()
分配 TextBlock?
**** 编辑 ****好的,我的代码中可能发生了其他事情,因为我无法始终如一地重现此行为。如果找到根本原因,我会报告。
* 编辑 2 *我已经确认我有另一个问题导致了失火,而不是 OAPH。 .ToProperty 和 .BindTo 现在似乎一直在按预期工作。谢谢。
最佳答案
It will now fire off to the TextBlock because now there is a subscription. So I'm guessing that the .BindTo() method never actually counts as a subscription?
BindTo 立即订阅源并且应该启动 OAPH。但是,在 View 获得 ViewModel 之前,此订阅实际上发生:
// Can't subscribe to (null).Message!
this.WhenAnyValue(t => t.ViewModel.Message)
关于c# - ToProperty 和 BindTo - 无需订阅即可获取初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25252210/
我有一个问题,切换复选框不起作用,控制台显示如下。 代码: Me OpenStreetMap Toggle layer Visibility
我是 AngularJs 的新手,现在我正在尝试使用 Firebase。 这是我的 Firebase 数据: pavlovdog | ---facebook: "facebook.pavlovdog
我正在尝试在 Swift 2.0 中构建一个照片共享应用程序,我使用 carthage 将 SWIFTBOND 引入我的 xcode 项目,但不幸的是我遇到了这个问题。 var post: Post?
在 Swift3 中我遇到了一个问题: @IBOutlet weak var connectButton: UIButton! [...] viewModel.connectButtonTitle.a
当我尝试将第一个 map 的 View 绑定(bind)到第二个 map 时,收到此错误消息。所以,我有两张 map - mapOne 和 mapTwo。当我不进行任何绑定(bind)时,它们工作得很
例如,我有一些代码: class A { private $value = 100; } $a = new A; $closure = function(){echo $this->value
我最近开始为我的 Angular JS 探索 Firebase。我有两个片段, var ref = new Firebase("https://my-firebase-id.firebasei
我在 .NET 4.5 中使用 RXUI 6 和 WPF。 当它绑定(bind)到的 ViewModel 属性由 ObservableAsPropertyHelper 支持时,我一直无法获得提供给我的
我有一个读写属性 private string _serialNumber; public string SerialNumber { get => _seri
如何在对象上调用 angularfire 的 $bindTo() 后从 firebase 中删除对象。出于某种原因,调用 $bindTo() 似乎从对象中删除了 $remove() 函数。 例如,除非
userView = Marionette.View.extend tagName: "h1" initialize:-> @bindTo @model, "change:name",
ToProperty() 似乎用于基于可观察对象的只读属性,但我什么时候想使用 BindTo()? 最佳答案 ToProperty 用于 ViewModel,而 BindTo 更灵活一些,旨在用于应用
我正在尝试更新 View XAML 中元素的属性: this.WhenAnyValue(x => x.ViewModel.IsEnabled).BindTo(this, x => x.MyButton
我是学习 RxSwift 的新手。 我修改了 RxSwift Example App 中的 Simple Numbers 示例,它将三个数字添加到一个结果数字中。 我添加了一个 testStr UIT
我正在尝试将 Kendo AutoComplete 绑定(bind)到 View 模型。我似乎无法将其设置正确。这就是我尝试绑定(bind)到模型属性列表的方式 该属性是模型的集合,如下所示: @Ht
我是 OpenLayers 的新手。我正在尝试使用 3.0 和 3.13 版本做一些练习。 我必须链接两个 View :第二张 map 响应第一张 map 的变化,但缩小了三倍;当平移或缩放第一张 m
这次我正在努力使用不同的方法来绑定(bind)事件。我的代码中有所有提到的方法。我只是不知道,如果我走对了。也许我应该总是使用 bindTo 来确保我的 View 在更改后完全关闭(目前这通常会产生错
我试图通过在他们的官方页面上构建这个示例代码来取消绑定(bind) Open Layers 3 中的并排 map : http://ol3js.org/en/master/examples/side-
我正在学习 Kendo UI。我们正在努力将 Kendo UI 用于 MVC 应用程序 View 中的所有内容。 我正在尝试绑定(bind)到 Kendo 网格每一行中的模型对象内的集合。我们使用 E
我使用 RxSwift绑定(bind)我的 viewmodel至 UILabel和 UITexfield . UITextfield转换为 Swift 3 没有问题, 只是替换 rx_text与 rx
我是一名优秀的程序员,十分优秀!