UIView中的阴影无法覆盖Xamarin表单中的Entry或Editor或Picker

时间:2018-11-22 07:31:47

标签: ios xamarin.forms xamarin.ios effects dropshadow

我在Xamarin Form项目中添加了RoutingEffect,在Xamarin.iOS项目中添加了PlatformEffect。它将为Stacklayout添加效果。此演示中的Stacklayout是一个自定义导航栏。导航栏下面是一个滚动视图,其中包含许多单元格(标签,条目,选择器)。

我在Android中实现就可以了。 但是在iOS中存在问题:阴影效果无法覆盖某些控件,例如:Entry,Editor,Picker。你能告诉我如何解决吗? 这是Xamarin.iOS项目中的代码。

public class DropShadowEffect : PlatformEffect
{
    protected override void OnAttached()
    {
        try
        {
            var effect = (myDemo.UIControls.DropShadowEffect)Element.Effects.FirstOrDefault(e => e is myDemo.UIControls.DropShadowEffect);

            if (effect != null)
            {
                Container.Layer.CornerRadius = effect.Radius;
                Container.Layer.ShadowColor = UIColor.Red.CGColor;// effect.Color.ToCGColor();
                Container.Layer.ShadowOffset = new CGSize(effect.DistanceX, effect.DistanceY);
                Container.Layer.ShadowOpacity = 0.8f;
                Container.Layer.ShadowRadius = 2f;
                Container.Layer.ShouldRasterize = true;
                Container.Layer.MasksToBounds = false;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Cannot set property on attached control. Error: {0}", ex.Message);
        }
    }

*阴影效果过多标签没问题

Shadow effect overly Label is OK

*阴影效果无法覆盖选择器或条目

cannot overlay either Picker or Entry

1 个答案:

答案 0 :(得分:0)

原因:

实际上,诸如Label仍会覆盖阴影,但是看起来并不明显。如果设置标签的背景(例如red),则会看到覆盖。

解决方案:

您可以在自定义渲染器中设置BackgroundColorPicker中的Entry,以使Alpha为0。

例如EntryRenderer

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control != null)
        {
            Control.BackgroundColor = new UIColor(1,1,1,0);//The last parameter sets the alpha of backgound as transparent
            Control.Layer.MasksToBounds = true;
            Control.Layer.CornerRadius = xxx;  //set the rounded corner
            Control.Layer.BorderColor = UIColor.xxx.CGColor;
            Control.Layer.BorderWidth = xxx;           
        }

    }