c# - 如何在 Xamarin.forms 中的框架边缘显示阴影?

标签 c# xamarin xamarin.forms xamarin.android shadow

目前,我希望我的框架元素的边缘在 Xamarin XAML 中显示为阴影,使用框架元素的 HasShadow 属性,但它不起作用或标记差异,然后我将渲染一个自定义框架,如果它出来但它不是阴影,它把它放在第二行,我使用 Android 7。

Codigo XAML:

        <localframe:MyFrame 
           HasShadow="True" 
           ShadowColor="Red" 
           BorderColor="Red"
           BorderWidth="10"/>

代码框架:

public class MyFrame:Frame
    {

        public static readonly BindableProperty ShadowColorProperty = BindableProperty.Create(nameof(ShadowColor), typeof(Color), typeof(MyFrame), Color.Transparent);
        public static readonly BindableProperty BorderWidthProperty = BindableProperty.Create(nameof(BorderWidth), typeof(float), typeof(MyFrame));

        public Color ShadowColor
        {
            get { return (Color)GetValue(ShadowColorProperty); }
            set { SetValue(ShadowColorProperty, value); }
        }


        public float BorderWidth
        {
            get { return (float)GetValue(BorderWidthProperty); }
            set { SetValue(BorderWidthProperty, value); }
        }

    }

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);
        }


    }
}

enter image description here

最佳答案

我相信这是一个existing issue在 Xamarin.Forms 中。他们还在 this thread 中提到了 Android 问题。 HasShadow 对 Android 没有影响。

您可以使用PancakeView这是一个非常好的、功能齐全的 Frame 替代品。

关于c# - 如何在 Xamarin.forms 中的框架边缘显示阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62565327/

相关文章:

c# - Xamarin.Forms 的属性注入(inject)(setter 注入(inject)) Prism

xamarin - 如何使 ActivityIndi​​cator 覆盖全屏?

Xamarin Forms 导航堆栈

c# - 重构大型代码隐藏 - 存储库模式和扩展方法

c# - 如何在运行时初始化包含工件的所有静态类的列表,以便能够使用其参数动态调用其方法?

javascript - 在 MainActivity Xamarin 中从 webview 调用 javascript

c# - 将数据绑定(bind)到 xamarin 中的子 Listview 元素

c# - 如何解析文本文件?

c# - 如何验证 SMTP 服务器

Xamarin 在 XAML 中更改字体