我有一个客户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>
答案 0 :(得分:2)
如果您在调度程序线程上观察流,应该可以工作:
public AppViewModel(MyClient client)
{
Client = client;
Client.StatusStream().ObserveOnDispatcher().ToPropertyEx(this, x => x.Status);
}