我试图学习一致的反应,所以我想为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);
}
}
所以问题:
如果我使用上面的注释绑定没有任何反应。因此,progress属性正在正确更改,但ViewModel的Percentage属性永远不会被设置。绑定正在从viewmodel工作到视图,但不是其他方式(EditText绑定在两个方向都正常工作)。如何解决这个问题?
如果我从ProgressChanged事件创建observable,我设法使它工作。但即使我不得不做这样的事情,我也不确定这是不是正确的方法。当活动被破坏或不被破坏时,我还必须处理订阅吗?
最后一件事。如果我想学习这个框架,最好的方法是什么?
答案 0 :(得分:3)
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);