xamarin.forms默认情况下,如果属性是double?类型,绑定到Entry上,是无法实现双向绑定的,
可以自定义Converter实现双向绑定
publicclass NullableConverter : IValueConverter
{
publicobjectConvert(objectvalue, Type targetType,object parameter, CultureInfo culture)
{
return System.Convert.ChangeType(value, targetType);
}
publicobjectConvertBack(objectvalue, Type targetType,object parameter, CultureInfo culture)
{
if(value ==null)
returnnull;
if( targetType.IsGenericType )
{
Type valueType = targetType.GetGenericArguments()[0];
objectresult = System.Convert.ChangeType(value, valueType);
return Activator.CreateInstance(targetType, result);
}
return System.Convert.ChangeType(value, targetType);
}
}
然后在app.xaml里面,加入资源配置
然后,以后Entry绑定nullable类型时,可以这样写
Text="{Binding propertyName,Converter={StaticResource NullableConverter}}"
网友评论