以下是我要做的事情......在我的视图模型中,我有以下方法:
private bool AllValuesFilledIn()
{
return Inspection.InspectionValueCategories.All(category => !category.InspectionValues.Any(value => string.IsNullOrEmpty(value.Value)));
}
然后我使用此方法启用和禁用导航到下一个屏幕的按钮。 (即我在你进入下一个屏幕之前验证你已经填满了所有内容)就像这样:
public ICommand NextScreenCommand
{
get { return new MvxCommand(async () => await ExecuteNextScreenCommand(), AllValuesFilledIn); }
}
我的问题是,默认情况下,更改表格单元格上的这个值不会更新UI告诉它在填充所有内容时重新启用按钮。我想我可以通过让表格单元格发布一个来完成此操作视图模型正在侦听的消息。像这样:
细胞:
public partial class InspectionCell : MvxTableViewCell
{
public static readonly UINib Nib = UINib.FromName("InspectionCell", NSBundle.MainBundle);
public static readonly NSString Key = new NSString("InspectionCell");
public InspectionCell(IntPtr handle)
: base(handle)
{
this.DelayBind(() => {
var set = this.CreateBindingSet<InspectionCell, InspectionValue>();
set.Bind(DescriptionLabel).To(v => v.Description);
set.Bind(ValueTextField).To(v => v.Value);
set.Apply();
});
}
public static InspectionCell Create()
{
return (InspectionCell)Nib.Instantiate(null, null)[0];
}
public override void AwakeFromNib()
{
base.AwakeFromNib();
ValueTextField.EditingChanged += OnValueTextFieldOnEditingDidEnd;
}
private void OnValueTextFieldOnEditingDidEnd(object sender, EventArgs args)
{
Mvx.Resolve<IMvxMessenger>().Publish(new InspectionViewUpdateMessage(this));
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
{
return;
}
var textField = ValueTextField;
if (textField != null)
{
textField.EditingChanged -= OnValueTextFieldOnEditingDidEnd;
}
}
}
视图模型:
public class InspectionViewModel : BaseViewModel
{
private IDisposable updateToken;
private Inspection inspection;
public Inspection Inspection
{
get { return inspection; }
set
{
inspection = value;
RaisePropertyChanged(() => Inspection);
}
}
public ICommand NextScreenCommand
{
get { return new MvxCommand(async () => await ExecuteNextScreenCommand(), AllInspectionValuesFilledIn); }
}
public InspectionViewModel()
{
updateToken = Mvx.Resolve<IMvxMessenger>().SubscribeOnMainThread<InspectionViewUpdateMessage>(OnUpdate);
}
private void OnUpdate(MvxMessage obj)
{
RaisePropertyChanged(() => Inspection);
RaisePropertyChanged(() => NextScreenCommand);
}
private bool AllInspectionValuesFilledIn()
{
return Inspection.InspectionValueCategories.All(category => !category.InspectionValues.Any(value => string.IsNullOrEmpty(value.Value)));
}
这个几乎有效...每当我尝试更改单元格上的值时,它确实会发布我的消息..但问题是,messenger发布导致文本实际上不是填写在牢房里。我点击单元格,我开始打字,没有任何东西被填写,但事件发生了。
我错过了什么?是否有更好的方法来实现这一目标?