gpt4 book ai didi

c# - Reactiveui 绑定(bind)不适用于 SeekBar.Progress

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:13 25 4
gpt4 key购买 nike

我正在尝试学习 reactiveui,所以我想为 android 制作简单的小费计算器应用程序。 View 模型是:

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

Activity 看起来像这样:

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

所以问题:

  • 如果我使用上面注释的绑定(bind),则不会发生任何事情。因此 progress 属性正在正确更改,但从未设置 ViewModel 的 Percentage 属性。绑定(bind)从 View 模型到 View 工作,但不是其他方式(EditText 绑定(bind)在两个方向上都工作正常)。如何解决这个问题?

  • 如果我从 ProgressChanged 事件创建可观察对象,我设法让它工作。但即使我不得不做这样的事情,我也不确定这是否是正确的方法。另外,当 Activity 被手动销毁时,我是否必须处理订阅?

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

最佳答案

SeekBar 很可能不在受支持的现成控件列表中; Android 没有内置的方式来观察 View 属性,因此我们必须将它们硬编码为一次性的。如果您编写类似以下内容,则可以完成这项工作:

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

关于c# - Reactiveui 绑定(bind)不适用于 SeekBar.Progress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com