如何在XAML Style中设置Clip属性实现视图裁剪?
你需要把Clip对应的EllipseGeometry对象嵌套在Setter.Value节点内赋值,完整的可复用样式代码如下:
<Style TargetType="Image" ApplyToDerivedTypes="True" x:Key="roundedImageStyle"> <Setter Property="HeightRequest" Value="75"/> <Setter Property="WidthRequest" Value="75"/> <Setter Property="Aspect" Value="AspectFit"/> <Setter Property="Clip"> <Setter.Value> <EllipseGeometry Center="37.5,37.5" RadiusX="37.5" RadiusY="37.5" /> </Setter.Value> </Setter> </Style>
使用时直接给Image控件绑定样式即可,无需重复写裁剪逻辑:
<Image Style="{StaticResource roundedImageStyle}" Source="图片资源路径"/>
如果后续需要调整圆形图像的尺寸,只需要同步修改HeightRequest、WidthRequest的值,以及EllipseGeometry的Center、RadiusX、RadiusY的数值为宽高的1/2即可。
内容的提问来源于stack exchange,提问作者Juan Sturla
相关产品推荐
相关产品推荐

