为了账号安全,请及时绑定邮箱和手机立即绑定

RX:如何将 IObservable<object> 绑定到属性 (ReactiveUI)

RX:如何将 IObservable<object> 绑定到属性 (ReactiveUI)

C#
LEATH 2022-12-24 14:27:53
我上课MyClient时有IObservable<IStatus>( Client.StatusStream())。现在我想结合ReactiveXand ReactiveUI。但是文档没有提供任何如何一起使用它们的示例。到目前为止,我已经尝试了一些扩展方法 (fe .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?    }}
查看完整描述

1 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

定义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);

    }

}


查看完整回答
反对 回复 2022-12-24
  • 1 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信