【发布时间】:2013-08-06 22:34:48
【问题描述】:
我想知道我是否以正确的方式执行此操作 - 此方法有效,但感觉有点“脏”。本质上,MvxTableViewCell 中的按钮会更改绑定对象的参数,但单元格不会更新以反映更改,直到它被滚动出视图并返回视图(即单元格被“重绘”)。这里所有的例子都被简化了,但你明白了..
首先,我的对象:
public class Expense
{
public decimal Amount { get; set; }
public bool Selected { get; set; }
public Command FlipSelected
{
get { return new MvxCommand(()=> this.Selected = !this.Selected); }
}
}
其次,我的单元格(在构造函数中)包含:
this.DelayBind(() =>
{
var set = this.CreateBindingSet<HistoryCell, Expense>();
set.Bind(this.TitleText).To(x => x.Amount);
set.Bind(this.SelectButton).To(x=> x.FlipSelected);
set.Bind(this.SelectButton).For(x => x.BackgroundColor).To(x => x.Selected).WithConversion(new ButtonConverter(), null);
set.Apply();
});
我有一个返回按钮背景颜色的值转换器:
class ButtonConverter : MvxValueConverter<bool, UIColor>
{
UIColor selectedColour = UIColor.FromRGB(128, 128, 128);
UIColor unSelectedColour = UIColor.GroupTableViewBackgroundColor;
protected override UIColor Convert(bool value, Type targetType, object parameter, CultureInfo culture)
{
return value ? selectedColour : unSelectedColour;
}
protected override bool ConvertBack(UIColor value, Type targetType, object parameter, CultureInfo culture)
{
return value == selectedColour;
}
}
是的,所以发生的情况是,如果我单击单元格中的按钮,它会运行翻转布尔值 Selected 的命令,这反过来又通过 ButtonConverter 值绑定回单元格的背景颜色转换器。
我遇到的问题是单元格不会立即更新 - 只有当我滚动出该单元格的视图并返回视图时(即重新绘制该单元格)。所以我想我只会让细胞变得“脏”:
this.SelectButton.TouchUpInside += (o, e) =>
{
this.SetNeedsDisplay();
};
但这不起作用。 的作用 是在手动更改背景颜色的TouchUpInside 事件中添加额外的代码。但我假设这不是正确的做法。
当我在Expense 对象中更改Selected 的值时,是否需要触发RaisePropertyChanged?当它只是一个对象时,我该怎么做?
真的希望 Stuart 能在这方面提供帮助;)
【问题讨论】:
标签: xamarin.ios xamarin mvvmcross