Reactiveui绑定不适用于SeekBar.Progress

本文关键字:SeekBar Progress 适用于 不适用 绑定 Reactiveui | 更新日期: 2023-09-27 18:30:02

我正在尝试学习reactiveui,所以我想为android制作一个简单的提示计算器应用程序。ViewModel是:

public class TipCalcViewModel : ReactiveObject
{
    [IgnoreDataMember] private double _subtotal;
    [DataMember]
    public double Subtotal
    {
        get { return _subtotal; }
        set { this.RaiseAndSetIfChanged(ref _subtotal, value); }
    }
    [IgnoreDataMember] private int _percentage;
    [DataMember]
    public int Percentage
    {
        get { return _percentage; }
        set { this.RaiseAndSetIfChanged(ref _percentage, value); }
    }
    [IgnoreDataMember] private readonly ObservableAsPropertyHelper<double> _tipAmount;
    [DataMember]
    public double TipAmount
    {
        get { return _tipAmount.Value; }
    }
    [IgnoreDataMember] private readonly ObservableAsPropertyHelper<double> _total;
    [DataMember]
    public double Total
    {
        get { return _total.Value; }
    }
    public TipCalcViewModel(ITipCalcService service)
    {
        _tipAmount = this.WhenAnyValue(
            x => x.Subtotal,
            y => y.Percentage,
            service.CalculateTipAmount)
            .ToProperty(this, vm => vm.TipAmount);
        _total = this.WhenAnyValue(
            x => x.Subtotal,
            y => y.Percentage,
            service.CalculateTotal)
            .ToProperty(this, vm => vm.Total);
    }
}

活动看起来是这样的:

public class TipCalcActivity : ReactiveActivity<TipCalcViewModel>
{
    public EditText SubTotal { get { return this.GetControl<EditText>(); } }
    public SeekBar Percentage { get { return this.GetControl<SeekBar>(); } }
    public TextView PercentageText { get { return this.GetControl<TextView>(); } }
    public TextView TipAmount { get { return this.GetControl<TextView>(); } }
    public TextView Total { get { return this.GetControl<TextView>(); } }
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        var service = new TipCalcService();
        ViewModel = new TipCalcViewModel(service);
        this.Bind(ViewModel, vm => vm.Subtotal, v => v.SubTotal.Text);
        //this.Bind(ViewModel, vm => vm.Percentage, v => v.Percentage.Progress);
        this.OneWayBind(ViewModel, vm => vm.Percentage, v => v.PercentageText.Text);
        this.OneWayBind(ViewModel, vm => vm.TipAmount, v => v.TipAmount.Text);
        this.OneWayBind(ViewModel, vm => vm.Total, v => v.Total.Text);
        var disposable = Observable.FromEventPattern<SeekBar.ProgressChangedEventArgs>(
            h => Percentage.ProgressChanged += h,
            h => Percentage.ProgressChanged -= h)
            .Throttle(TimeSpan.FromMilliseconds(500))
            .DistinctUntilChanged()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Subscribe(x => 
                ViewModel.Percentage = x.EventArgs.Progress);
    }
}

所以问题:

  • 如果我使用上面的注释绑定,则不会发生任何事情。所以进度属性正在正确更改,但ViewModel的Percentage属性从未设置。绑定是从视图模型到视图的,但不是从另一个方向(EditText绑定在两个方向上都很好)。如何解决此问题?

  • 如果我从ProgressChanged事件中创建可观察的内容,我设法使其工作。但即使我不得不这样做,我也不确定这是否是正确的方式。此外,当活动被手动销毁或未被手动销毁时,我是否必须处理订阅?

  • 还有最后一件事。如果我想学习这个框架,最好的开始方式是什么?

Reactiveui绑定不适用于SeekBar.Progress

SeekBar很可能不在支持的开箱即用控件列表中;Android没有内置的方法来观察视图属性,所以我们必须将它们硬编码为一次性的。如果你写这样的东西,你可以做到这一点:

var progressChanged = Observable.FromEventPattern<SeekBar.ProgressChangedEventArgs>(
        h => Percentage.ProgressChanged += h,
        h => Percentage.ProgressChanged -= h);
this.Bind(ViewModel, 
    vm => vm.Percentage, 
    v => v.Percentage.Progress, 
    signalViewUpdate: progressChanged);