RX: 'ObservableAsProperty' bound propertie

2019-08-29 07:55发布

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?!

1条回答
何必那么认真
2楼-- · 2019-08-29 08:55

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);
}
查看更多
登录 后发表回答