I am using RXUI 6 with WPF in .NET 4.5.
I'm having trouble getting the initial value provided to my view when the ViewModel property to which it is attached is supported by ObservableAsPropertyHelper .
According to the documentation :
ToProperty / OAPH changes
ObservableAsPropertyHelper is no longer an IObservable, use WhenAny to observe it.
ObservableAsPropertyHelper is now lazy. Subscribe to the source only when the value is read the first time. This greatly improves performance and memory usage, but at the cost of some โWhyโ is not my test work? "confusion. If you find that your ToProperty
"does not work", perhaps, therefore.
I considered this question , which seems to concern my own problem, but the answer provided works in testing and with ReactiveCommand . I cannot understand the cleanest way to make this work in my situation with any IObservable<> not necessarily a ReactiveCommand (simplified below).
ViewModel example:
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; } } }
Example View Code:
public partial class View : UserControl, IViewFor<ViewModel> { public View() { InitializeComponent(); this.WhenAnyValue(t => t.ViewModel.Message) .BindTo(this, t => t.MessageTextBlock.Text); }
So, the TextBox Message Message text will not contain the initial value. However, if in my ViewModel I had to add a line to the contructor:
this.WhenAnyValue(t => t.Message).Subscribe(s => {});
Now it will be launched in TextBlock, because now there is a subscription. So I assume that the .BindTo() method is never considered a subscription? Or is it laziness at the top of laziness? Does this empty subscription mean performance benefits from her laziness? Or should I not use .BindTo() and just use .Subscribe() to assign a TextBlock?
**** EDIT **** Okay, there might be something else in my code as I was unable to reproduce this behavior sequentially. I will return the report if I find the root cause.
* EDIT 2 * I confirmed that I was having another issue that caused skips, not OAPH. It seems that .ToProperty and .BindTo work in sequence as expected. Thanks.