RX:'ObservableAsProperty'绑定的属性不会在gui上自动更新

时间:2019-04-23 12:07:51

标签: c# wpf reactiveui

我有一个客户MyClient,该客户通过IObservable提供了新的状态。如文档(https://reactiveui.net/docs/handbook/view-models/boilerplate-code中所述,我制作了一个ObservableAsProperty类型的私有属性来获取IObservable的输出。但要知道我的GUI不会根据更改自动更新。

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; }
}

如果我通过xaml将gui与DataContext绑定在一起,那么一切都会按预期进行。 ViewModel.PropertyChanged被解雇了,所以我不知道那是怎么回事?!

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>

1 个答案:

答案 0 :(得分:2)

如果您在调度程序线程上观察流,应该可以工作:

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