【发布时间】:2020-07-11 02:56:07
【问题描述】:
我创建了一个基于谷歌搜索的自定义渲染,它允许Frame 完全控制其所有角半径(左、上、右、下)。但是,即使它设置为60, 60, 0, 0,我也认为它不尊重这些标准。在 Android 上,它按预期工作。
iOS 屏幕:
安卓:
在调试时,我可以清楚地看到自定义渲染适当地获取了数据,即角半径被相应地传递下来。这是自定义渲染器代码:
public override void LayoutSubviews()
{
base.LayoutSubviews();
UpdateCornerRadius();
UpdateShadow();
}
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
if (e.NewElement == null)
return;
UpdateShadow();
UpdateCornerRadius();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(MultiCornerFrame.CornerRadius) || e.PropertyName == nameof(MultiCornerFrame))
{
UpdateCornerRadius();
}
if(e.PropertyName == nameof(MultiCornerFrame.Elevation))
{
UpdateShadow();
}
}
private void UpdateShadow()
{
var materialFrame = (MultiCornerFrame) Element;
// Update shadow to match better material design standards of elevation
Layer.ShadowRadius = materialFrame.Elevation;
Layer.ShadowColor = UIColor.Gray.CGColor;
Layer.ShadowOffset = new CGSize(2, 2);
Layer.ShadowOpacity = 0.80f;
Layer.ShadowPath = UIBezierPath.FromRect(Layer.Bounds).CGPath;
Layer.MasksToBounds = false;
}
// A very basic way of retrieving same one value for all of the corners
private double RetrieveCommonCornerRadius(CornerRadius cornerRadius)
{
var commonCornerRadius = cornerRadius.TopLeft;
if (commonCornerRadius <= 0)
{
commonCornerRadius = cornerRadius.TopRight;
if (commonCornerRadius <= 0)
{
commonCornerRadius = cornerRadius.BottomLeft;
if (commonCornerRadius <= 0)
{
commonCornerRadius = cornerRadius.BottomRight;
}
}
}
return commonCornerRadius;
}
private UIRectCorner RetrieveRoundedCorners(CornerRadius cornerRadius)
{
var roundedCorners = default(UIRectCorner);
if (cornerRadius.TopLeft > 0)
{
roundedCorners |= UIRectCorner.TopLeft;
}
if (cornerRadius.TopRight > 0)
{
roundedCorners |= UIRectCorner.TopRight;
}
if (cornerRadius.BottomLeft > 0)
{
roundedCorners |= UIRectCorner.BottomLeft;
}
if (cornerRadius.BottomRight > 0)
{
roundedCorners |= UIRectCorner.BottomRight;
}
return roundedCorners;
}
private void UpdateCornerRadius()
{
var cornerRadius = (Element as MultiCornerFrame)?.CornerRadius;
if (!cornerRadius.HasValue)
{
return;
}
var roundedCornerRadius = RetrieveCommonCornerRadius(cornerRadius.Value);
if (roundedCornerRadius <= 0)
{
return;
}
var roundedCorners = RetrieveRoundedCorners(cornerRadius.Value);
var path = UIBezierPath.FromRoundedRect(Bounds, roundedCorners, new CGSize(roundedCornerRadius, roundedCornerRadius));
var mask = new CAShapeLayer { Path = path.CGPath};
NativeView.Layer.Mask = mask;
}
从中调用它的 XAML 如下所示:
<customcontrols:MultiCornerFrame Elevation="48" CornerRadius="60, 60, 0, 0" Grid.Row="1" BackgroundColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
如果我需要发布更多代码,请告诉我
【问题讨论】:
-
您好,您是否检查过在渲染器代码中添加断点以检查
UpdateCornerRadius();是否运行。
标签: c# xaml xamarin.forms custom-renderer