我想在RelativeLayout
内居中放置图像
X点的等式应为=(父级宽度-视图的宽度)/ 2 ..
我试图用XAML编写它:
<RelativeLayout x:Name="rel1">
<Image x:Name="image1" Aspect="AspectFit"
RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
Constant={Binding Source=rel1, Path=Width, Converter={StaticResource centerFromWidth},
ConverterParameter={Binding Source=image1, Path=Width}}}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height, Factor=0.5, Constant=0}"
Source="{Binding File1, Converter={StaticResource base64ToImage}}">
</Image>
<RelativeLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OpenImagePopupCommand}" CommandParameter="1"/>
</RelativeLayout.GestureRecognizers>
</RelativeLayout>
这是转换器:
public class FromDimensionToCenter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var parentWidth = (double)value;
var viewWidth = (double)parameter;
return (parentWidth - viewWidth) / 2;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
但是我得到这个错误:
未找到“常量”的属性,可绑定属性或事件,或者 值和属性之间的类型不匹配。