【发布时间】:2017-12-28 13:57:45
【问题描述】:
我有一个带有自定义 BindableProperty 的自定义视图:
public string Valore {
get { return (string)GetValue(ValoreProperty); }
set { SetValue(ValoreProperty, value); }
}
public static readonly BindableProperty ValoreProperty =
BindableProperty.Create(
propertyName: nameof(Valore),
returnType: typeof(string),
declaringType: typeof(VolosTimePickerView),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: ValorePropertyChanged,
propertyChanging: ValorePropertyChanging
);
在我的 XAML 中,由于 Valore 的值是可以为空的 Int,我必须使用这样的转换器:
<view:VolosTimePickerView Grid.Row="0" Grid.Column="1"
Valore="{Binding OrariSelezionati.ViaggioInizioMattina, Converter={StaticResource NullableConverter}}" />
如何在 XAML 端不使用 Converter={StaticResource NullableConverter 而是直接在属性后面的代码中做同样的事情?
谢谢!
【问题讨论】:
-
为什么需要它成为
BindableProperty?为什么不能是实现INotifyPropertyChanged的常规属性? -
为什么需要在代码隐藏中设置转换器?您是否考虑过公开 Nullable-Int BindableProperty,然后在 ValorePropertyChanged 事件处理程序中更新常规字符串属性的值?在这种情况下,您不需要转换器,并且逻辑在 ContentView 中实现。相反,如果您真的想从代码隐藏中设置转换器,请尝试查看forums.xamarin.com/discussion/18849/…