IOS 来自框架的 CardView 的自定义呈现
Custom Rendering on IOS a CardView from Frame
我正在尝试通过子类化 Frame 并制作自定义渲染器在 IOS Xamarin Forms 上制作 CardView。我想实现这样的目标:
在查看 API 设置阴影时,我这样做了:
[assembly: ExportRenderer(typeof(CardView), typeof(CardViewRenderer))]
namespace TrabbleMobile.iOS.CustomRenderers
{
public class CardViewRenderer : FrameRenderer
{
public override void Draw(CGRect rect)
{
var cardView = (CardView)this.Element;
using (var context = UIGraphics.GetCurrentContext())
{
//nfloat cornerRadius = 2;
float shadowOffsetWidth = 2;
float shadowOffsetHeight = 4;
var shadowColor = new CGColor(0, 0, 0, (nfloat)0.5);
var boxColor = new CGColor(255, 255, 255);
var shadowBlur = (float)0.5;
var offset = new CGSize(shadowOffsetWidth, shadowOffsetHeight);
context.SetShadow(offset, shadowBlur, shadowColor);
但是,它没有按应有的方式渲染,根本没有阴影。
我已经这样做了,我这样做的方式是通过 IOS 上的自定义渲染,这里是自定义渲染器代码:
public class CardViewRenderer : FrameRenderer
{
public override void Draw(CGRect rect)
{
SetupShadowLayer();
base.Draw(rect);
}
void SetupShadowLayer()
{
Layer.CornerRadius = 2; // 5 Default
if (Element.BackgroundColor == Xamarin.Forms.Color.Default)
{
Layer.BackgroundColor = UIColor.White.CGColor;
}
else
{
Layer.BackgroundColor = Element.BackgroundColor.ToCGColor();
}
Layer.ShadowRadius = 2; // 5 Default
Layer.ShadowColor = UIColor.Black.CGColor;
Layer.ShadowOpacity = 0.4f; // 0.8f Default
Layer.ShadowOffset = new CGSize(0f, 2.5f);
if (Element.OutlineColor == Xamarin.Forms.Color.Default)
{
Layer.BorderColor = UIColor.Clear.CGColor;
}
else
{
Layer.BorderColor = Element.OutlineColor.ToCGColor();
Layer.BorderWidth = 1;
}
Layer.RasterizationScale = UIScreen.MainScreen.Scale;
Layer.ShouldRasterize = true;
}
}
我正在尝试通过子类化 Frame 并制作自定义渲染器在 IOS Xamarin Forms 上制作 CardView。我想实现这样的目标:
在查看 API 设置阴影时,我这样做了:
[assembly: ExportRenderer(typeof(CardView), typeof(CardViewRenderer))]
namespace TrabbleMobile.iOS.CustomRenderers
{
public class CardViewRenderer : FrameRenderer
{
public override void Draw(CGRect rect)
{
var cardView = (CardView)this.Element;
using (var context = UIGraphics.GetCurrentContext())
{
//nfloat cornerRadius = 2;
float shadowOffsetWidth = 2;
float shadowOffsetHeight = 4;
var shadowColor = new CGColor(0, 0, 0, (nfloat)0.5);
var boxColor = new CGColor(255, 255, 255);
var shadowBlur = (float)0.5;
var offset = new CGSize(shadowOffsetWidth, shadowOffsetHeight);
context.SetShadow(offset, shadowBlur, shadowColor);
但是,它没有按应有的方式渲染,根本没有阴影。
我已经这样做了,我这样做的方式是通过 IOS 上的自定义渲染,这里是自定义渲染器代码:
public class CardViewRenderer : FrameRenderer
{
public override void Draw(CGRect rect)
{
SetupShadowLayer();
base.Draw(rect);
}
void SetupShadowLayer()
{
Layer.CornerRadius = 2; // 5 Default
if (Element.BackgroundColor == Xamarin.Forms.Color.Default)
{
Layer.BackgroundColor = UIColor.White.CGColor;
}
else
{
Layer.BackgroundColor = Element.BackgroundColor.ToCGColor();
}
Layer.ShadowRadius = 2; // 5 Default
Layer.ShadowColor = UIColor.Black.CGColor;
Layer.ShadowOpacity = 0.4f; // 0.8f Default
Layer.ShadowOffset = new CGSize(0f, 2.5f);
if (Element.OutlineColor == Xamarin.Forms.Color.Default)
{
Layer.BorderColor = UIColor.Clear.CGColor;
}
else
{
Layer.BorderColor = Element.OutlineColor.ToCGColor();
Layer.BorderWidth = 1;
}
Layer.RasterizationScale = UIScreen.MainScreen.Scale;
Layer.ShouldRasterize = true;
}
}