[英]RX: How to bind an IObservable<object> to a property (ReactiveUI)
I have class MyClient
with an IObservable<IStatus>
( Client.StatusStream()
). 我有一个
IObservable<IStatus>
Client.StatusStream()
IObservable<IStatus>
( Client.StatusStream()
)类MyClient
。 Now I would like to combine ReactiveX
and ReactiveUI
. 现在,我想结合使用
ReactiveX
和ReactiveUI
。 But the documentation is not providing any example how to use them together. 但是文档没有提供如何一起使用它们的任何示例。
I have tried some extension methods so far (fe .ToProperty
), but they are not working. 到目前为止,我已经尝试了一些扩展方法(
.ToProperty
),但是它们无法正常工作。
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?
}
}
To notify the gui about new updates, to have to use ObserveOnDispatcher
, see https://stackoverflow.com/a/55811495/6229375 要通知gui有关新的更新,必须使用
ObserveOnDispatcher
,请参阅https://stackoverflow.com/a/55811495/6229375
Define Status
as an output property : 将
Status
定义为输出属性 :
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.