C# 如何在Xamarin.forms中显示边框边缘上的阴影?
目前,我希望我的框架元素的边缘在Xamarin XAML中显示为着色,使用框架元素的Hassadow属性,但它不起作用或标记差异,然后我将渲染自定义框架,如果它出来但不是着色,则将其作为第二行,我使用Android 7 Codigo XAML:C# 如何在Xamarin.forms中显示边框边缘上的阴影?,c#,xamarin,xamarin.forms,xamarin.android,shadow,C#,Xamarin,Xamarin.forms,Xamarin.android,Shadow,目前,我希望我的框架元素的边缘在Xamarin XAML中显示为着色,使用框架元素的Hassadow属性,但它不起作用或标记差异,然后我将渲染自定义框架,如果它出来但不是着色,则将其作为第二行,我使用Android 7 Codigo XAML: <localframe:MyFrame HasShadow="True" ShadowColor="Red" Borde
<localframe:MyFrame
HasShadow="True"
ShadowColor="Red"
BorderColor="Red"
BorderWidth="10"/>
Android中的代码渲染帧:
[assembly: ExportRenderer(typeof(MyFrame), typeof(FrameRendererMy))]
namespace xxxxxxxxxx.Droid
{
[Obsolete]
public class FrameRendererMy : FrameRenderer
{
public FrameRendererMy(Context context) : base(context)
{
}
protected override void OnDraw(Canvas canvas)
{
var frame = Element as MyFrame;
var my1stPaint = new Android.Graphics.Paint();
var my2ndPaint = new Android.Graphics.Paint();
var backgroundPaint = new Android.Graphics.Paint();
my1stPaint.AntiAlias = true;
my1stPaint.SetStyle(Paint.Style.Stroke);
my1stPaint.StrokeWidth = frame.BorderWidth + 2;
my1stPaint.Color = frame.BorderColor.ToAndroid();
my2ndPaint.AntiAlias = true;
my2ndPaint.SetStyle(Paint.Style.Stroke);
my2ndPaint.StrokeWidth = frame.BorderWidth;
my2ndPaint.Color = frame.BackgroundColor.ToAndroid();
backgroundPaint.SetStyle(Paint.Style.Stroke);
backgroundPaint.StrokeWidth = 4;
backgroundPaint.Color = frame.BackgroundColor.ToAndroid();
Rect oldBounds = new Rect();
canvas.GetClipBounds(oldBounds);
RectF oldOutlineBounds = new RectF();
oldOutlineBounds.Set(oldBounds);
RectF myOutlineBounds = new RectF();
myOutlineBounds.Set(oldBounds);
myOutlineBounds.Top += (int)my2ndPaint.StrokeWidth + 3;
myOutlineBounds.Bottom -= (int)my2ndPaint.StrokeWidth + 3;
myOutlineBounds.Left += (int)my2ndPaint.StrokeWidth + 3;
myOutlineBounds.Right -= (int)my2ndPaint.StrokeWidth + 3;
canvas.DrawRoundRect(oldOutlineBounds, 10, 10, backgroundPaint); //to "hide" old outline
canvas.DrawRoundRect(myOutlineBounds, frame.CornerRadius, frame.CornerRadius, my1stPaint);
canvas.DrawRoundRect(myOutlineBounds, frame.CornerRadius, frame.CornerRadius, my2ndPaint);
base.OnDraw(canvas);
}
}
}
我相信这是一个以Xamarin.Forms形式出现的问题。他们还提到了Hassadow中的Android问题,Hassadow对Android没有影响
您可以使用它,这是一个非常好的、功能齐全的替代框架
试试这段代码
<StackLayout Margin="10">
<Frame CornerRadius="5" Padding="8" HasShadow="True" Margin="10">
<StackLayout>
<Label Text="Frame Example" FontSize="Medium" FontAttributes="Bold" />
<BoxView Color="Gray" HeightRequest="2" HorizontalOptions="Fill" />
<Label Text="Frames can wrap more complex layouts"/>
</StackLayout>
</Frame>
</StackLayout>
如果不起作用,请检查下面的答案
边框绘制圆角矩形?如果你能提供一个屏幕截图或其他简单的关于你的需求?
<StackLayout Margin="10">
<Frame CornerRadius="5" Padding="8" HasShadow="True" Margin="10">
<StackLayout>
<Label Text="Frame Example" FontSize="Medium" FontAttributes="Bold" />
<BoxView Color="Gray" HeightRequest="2" HorizontalOptions="Fill" />
<Label Text="Frames can wrap more complex layouts"/>
</StackLayout>
</Frame>
</StackLayout>