我正在使用Xamarin.iOS和MvvmCross来构建页面视图。我在DateAndTime模式下有一个包含UIDatePicker的UIView。如果我在绑定的vm属性中放置断点并更改日期,我可以看到绑定火。如果我更改时间,但绑定属性上没有更新任何内容。
以下是代码:
[Register("EditJobViewJobView")]
public class EditJobView : MvxViewController
{
public new EditJobViewModel ViewModel
{
get { return (EditJobViewModel)base.ViewModel; }
}
private const float _leftMargin = 6;
private const float _labelHeight = 20;
private const float _pickerHeight = 28;
private readonly UIFont _labelFont = UIFont.BoldSystemFontOfSize(18f);
private readonly UIFont _controlFont = UIFont.SystemFontOfSize(18f);
private readonly UIView _paddingInsert = new UIView(new RectangleF(0, 0, 4, 0));
private int _currentTop = 0;
public override void ViewDidLoad()
{
base.ViewDidLoad();
View = new UIView() { BackgroundColor = UIColor.White };
NavigationItem.SetRightBarButtonItem(new UIBarButtonItem("Save", UIBarButtonItemStyle.Bordered,
(sender, args) => ViewModel.OkCommand.Execute(null)), true);
// ios7 layout
if (RespondsToSelector(new Selector("edgesForExtendedLayout")))
EdgesForExtendedLayout = UIRectEdge.None;
Title = "Edit Job";
AddLabel("Start On");
UIDatePicker startOnPicker;
var startOnView = AddDateTimeView(out startOnPicker);
var set = this.CreateBindingSet<EditJobView, EditJobViewModel>();
set.Bind(startOnPicker).For("DateAndTime").To(vm => vm.TestStartOn);
set.Bind(startOnView).To(vm => vm.TestFormatted);
set.Apply();
}
private void AddLabel(string caption)
{
_currentTop += 10;
var frame = new RectangleF(_leftMargin, _currentTop, 300, _labelHeight);
var label = new UILabel(frame);
label.Font = _labelFont;
label.Text = caption;
AddView(label);
_currentTop += 2;
}
private UITextField AddDateTimeView(out UIDatePicker datePicker)
{
var textField = AddTextField();
datePicker = new UIDatePicker();
datePicker.Mode = UIDatePickerMode.DateAndTime;
textField.InputView = datePicker;
return textField;
}
private UITextField AddTextField()
{
var frame = new RectangleF(_leftMargin, _currentTop, 300, _pickerHeight);
var textField = new UITextField(frame);
textField.Layer.BorderColor = UIColor.Black.CGColor;
textField.Layer.BorderWidth = 1f;
textField.Font = _controlFont;
textField.LeftView = _paddingInsert;
textField.LeftViewMode = UITextFieldViewMode.Always;
AddView(textField);
return textField;
}
private void AddView(UIView view)
{
View.AddSubview(view);
_currentTop += (int)view.Frame.Height;
}
}
有什么想法吗?
更新
这是有效的自定义绑定代码:
public class DatePickerDateAndTimeTargetBinding : MvxConvertingTargetBinding
{
public DatePickerDateAndTimeTargetBinding(object target) : base(target)
{
}
public override Type TargetType
{
get { return typeof(DateTime); }
}
protected UIDatePicker DatePicker
{
get { return (UIDatePicker) Target; }
}
public override void SubscribeToEvents()
{
DatePicker.ValueChanged += DatePickerValueChanged;
}
private void DatePickerValueChanged(object sender, EventArgs e)
{
var target = Target as UIDatePicker;
if (target == null)
return;
var value = GetDate(target);
FireValueChanged(value);
}
protected override void SetValueImpl(object target, object value)
{
var datePicker = (UIDatePicker) target;
datePicker.Date = (DateTime)value;
}
private DateTime GetDate(UIDatePicker datePicker)
{
var dateTime = DateTime.SpecifyKind(datePicker.Date, DateTimeKind.Unspecified);
var localDateTime = dateTime.ToLocalTime();
return localDateTime;
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.TwoWay; }
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
var target = Target as UIDatePicker;
if (target != null)
{
target.ValueChanged -= DatePickerValueChanged;
}
}
base.Dispose(isDisposing);
}
}
以下是连接它的启动代码:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterCustomBindingFactory<UIDatePicker>(
"DateAndTime",
binary => new DatePickerDateAndTimeTargetBinding(binary));
base.FillTargetFactories(registry);
}
答案 0 :(得分:2)
Date
绑定仅绑定Date
- 请参阅https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target/MvxUIDatePickerDateTargetBinding.cs中的来源
如果您想创建自己的自定义DateAndTime
绑定,那么使用来自https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target/MvxUIDatePickerDateTargetBinding.cs和https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target/MvxUIDatePickerTimeTargetBinding.cs的源代码应该非常容易 - 您可以在注册期间注册自定义绑定设置(有关如何注册自定义绑定,请参阅N + 1个视频N = 28)