【问题标题】:SetValue for BindableProperty doesn't trigger PropertyChanged event when set to nullBindableProperty 的 SetValue 设置为 null 时不会触发 PropertyChanged 事件
【发布时间】:2019-02-27 15:09:07
【问题描述】:

在我开发的 Xamarin.Forms 项目中,我创建了一个与 DateTime 一起使用的 NullableDatePicker?价值。在 XF 3.3 之前,DatePicker 能够设置 DateTime? ViewModel 的属性为 null。这不再适用于 XF 3.5 现在,当我的平台渲染器将 XF 控件的 BindableProperty 设置为 null 时,视图模型的绑定属性不会更新。将值更改为 DateTime 有效。

搜索 XF 3.3 和当前 XF 3.5 之间的更改并没有为绑定带来任何重大更改,我希望 stackoverflow 上的人可以提供帮助。我已经搜索了很多可能导致我的问题的原因。

public class ViewModel {
    private DateTime? _date;
    public DateTime? Date{
        get => _date;
        set => SetProperty(ref _date, value);
    }
}

NullableDatePicker : DatePicker {
    public static readonly BindableProperty NullableDateProperty = BindableProperty.Create(nameof(NullableDate), typeof(DateTime?), typeof(NullableDatePicker), null, BindingMode.TwoWay);

public class DateTime? NullableDate
    {
        get { return (DateTime?)GetValue(NullableDateProperty); }
        set { SetValue(NullableDateProperty, value); }
    }
}

public class NullableDatePickerRenderer : DatePickerRenderer
{
    ...
    public void OnClearDateTimeSelection(){
        ((IElementController) Element).SetValueFromRenderer(NullableDatePicker.NullableDateProperty, null);
    ...
    }
}

【问题讨论】:

  • 你可以提供一个最小的、完整的、可验证的例子,我帮你查。^.^

标签: c# mvvm xamarin.forms


【解决方案1】:

从 3.4.0.1008975 更新到 3.5.X 时,我遇到了同样的问题。我还没有找到原因,但我也不需要更新,这就是我向你推荐的。

3.5.X 版基本上为 XF 添加了可绑定布局。如果你不需要它,我建议你降级到 3.4.0.1008975 它会工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多