0

I have a Client MyClient which provides new stati via an IObservable. As described in the documentation (https://reactiveui.net/docs/handbook/view-models/boilerplate-code) I made a private property of type ObservableAsProperty to get the output of my IObservable. But know my GUI is not updated automatically on changes.

public partial class MainWindow : ReactiveWindow<AppViewModel>
{
    public MainWindow()
    {
        InitializeComponent();
        ViewModel = App.Container.GetInstance<AppViewModel>();
        this.WhenActivated(r =>
            {
                this.OneWayBind(ViewModel, viewModel => viewModel.Status.AoiStatus, view => view.StatusTxtbl.Text).DisposeWith(r);
            });
    }
}

public class AppViewModel : ReactiveObject
{

    public IStatus Status { [ObservableAsProperty] get; }

    public MyClient Client { get; }

    public AppViewModel(MyClient client)
    {
        Client = client;
        Client.StatusStream().ToPropertyEx(this, x => x.Status);
    }
}

public interface IStatus
{
    string AoiStatus { get; }
}

If I bind the gui via xaml with my DataContext, everything works as expected. ViewModel.PropertyChanged is fired, so I don't know what's wrong there?!

public partial class MainWindow : ReactiveWindow<AppViewModel>
{
    public MainWindow()
    {
        InitializeComponent();
        ViewModel = App.Container.GetInstance<AppViewModel>();
        DataContext = this;
    }
}

public class AppViewModel : ReactiveObject
{

    public IStatus Status { [ObservableAsProperty] get; }

    public MyClient Client { get; }

    public AppViewModel(MyClient client)
    {
        Client = client;
        Client.StatusStream().ToPropertyEx(this, x => x.Status);
    }
}

public interface IStatus
{
    string AoiStatus { get; }
}
<TextBlock>
    <Run Text="Status:" />
    <Run Text="{Binding ViewModel.Status.AoiStatus, Mode=OneWay}"/>
</TextBlock>
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77

1 Answers1

2

Should work if you observe the stream on the dispatcher thread:

public AppViewModel(MyClient client)
{
    Client = client;
    Client.StatusStream().ObserveOnDispatcher().ToPropertyEx(this, x => x.Status);
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Wonderful! But could you explain why I have to `ObserveOnDispatcher`? If I don't use it, the property is also set, the`propertyChangedEvent` is raised and `xaml` is working as expected. I'm confused.. – Dominic Jonas Apr 23 '19 at 12:49
  • 1
    @DominicJonas: Yes, but the `OneWayBind` throws when it tries to apply the binding to the `TextBlock` from a background thread. – mm8 Apr 23 '19 at 12:50