1

I have class MyClient with an IObservable<IStatus> (Client.StatusStream()). Now I would like to combine ReactiveX and ReactiveUI. But the documentation is not providing any example how to use them together.

I have tried some extension methods so far (f.e. .ToProperty), but they are not working.

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);
                this.OneWayBind(ViewModel, viewModel => viewModel.Status.OperationMode, view => view.OpModeTxtbl.Text).DisposeWith(r);
                this.OneWayBind(ViewModel, viewModel => viewModel.Status.TestPlan, view => view.TestplanTxtbl.Text).DisposeWith(r);
            });
    }

    private async void ButtonGetStatus_OnClick(object sender, RoutedEventArgs e)
    {
        // the manual mode to get the actual status information
        var status = await ViewModel.Client.GetStatusAsync();
        ViewModel.Status = status;
    }
}

public class AppViewModel : ReactiveObject
{
    private IStatus _Status;
    public IStatus Status
    {
        get => _Status;
        set => this.RaiseAndSetIfChanged(ref _Status, value);
    }

    public MyClient Client { get; }

    public AppViewModel(MyClient client)
    {
        Client = client;
        // automatically pushes every new status information
        Client.StatusStream(); // <- How to get the data out there?
    }
}

Info

To notify the gui about new updates, to have to use ObserveOnDispatcher, see https://stackoverflow.com/a/55811495/6229375

Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • 1
    If you go with ToProperty it’s just _status = Client.StatusStream().ToProperty(this, x => x.Status). ObservableAsPropertyHelper _status; public IStatus Status => _status.Value; – Colt Bauman Apr 23 '19 at 09:23
  • Thanks for your comment and also working solution. But now my properties are not updated anymore. Do you know why? – Dominic Jonas Apr 23 '19 at 11:43

1 Answers1

4

Define Status as an output property:

public class AppViewModel : ReactiveObject
{
    private readonly ObservableAsPropertyHelper<IStatus> _status;
    public string Status => _status.Value;

    public MyClient Client { get; }

    public AppViewModel(MyClient client)
    {
        Client = client;
        Client.StatusStream().ToProperty(this, x => x.Status, out _status);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks that work's so far. But now the properties are not automatically updated anymore. Do I have to change my `OneWayBind` syntax in the constructor of the `MainWindow`? – Dominic Jonas Apr 23 '19 at 10:25