Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

OnDetached being called when adding effect in iOS internally

$
0
0

I have an entry field to which am adding line color property and changing the entry box to one similar to android with just a line at the bottom of the field .
Works fine for android but in case of iOS It works fine but then OnDetached is being called internally . After property changed for all the properties are called somehow OnPropertyChanged is getting called again for property "renderer" which somehow then calls the OnDetached function after which OnAttached is called again .

Below is my Effect iOS code

public class EntryLineColorEffect : PlatformEffect
{
    UITextField control;

    protected override void OnAttached()
    {
        try
        {
            control = Control as UITextField;
            //else the line dissapears in the end .
           // UpdateLineColor();
        }
        catch (System.Exception ex)
        {
            Console.WriteLine("Cannot set property on attached control. Error: {0}", ex.Message);
        }
    }

    protected override void OnDetached()
    {
        control = null;
    }

    protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
    {
        base.OnElementPropertyChanged(args);

        if (args.PropertyName == LineColorBehavior.LineColorProperty.PropertyName || args.PropertyName == "Height")
        {
            Initialize();
            UpdateLineColor();
        }
    }

    private void Initialize()
    {
        if (Element is Entry entry)
        {
            Control.Bounds = new CGRect(0, 0, entry.Width, entry.Height);
        }
    }

    private void UpdateLineColor()
    {
        BorderLineLayer lineLayer = control.Layer.Sublayers.OfType<BorderLineLayer>()
                                                         .FirstOrDefault();

        if (lineLayer == null)
        {
            lineLayer = new BorderLineLayer();
            lineLayer.MasksToBounds = true;
            lineLayer.BorderWidth = 1.0f;
            control.Layer.AddSublayer(lineLayer);
            control.BorderStyle = UITextBorderStyle.None;
        }

        lineLayer.Frame = new CGRect(0f, Control.Frame.Height - 1f, Control.Bounds.Width, 1f);
        lineLayer.BorderColor = LineColorBehavior.GetLineColor(Element).ToCGColor();
        control.TintColor = control.TextColor;
    }

    private class BorderLineLayer : CALayer
    {
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>