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

Confused elements property in listview, after add new items

$
0
0

Hello, I can not fix the following hard bug:
I wrote a render for a frame with background gradient

public class CustomFrame : Frame
{
public static readonly BindableProperty StartColorProperty =
BindableProperty.Create(nameof(StartColor), typeof(Color),
typeof(CustomFrame), Color.White);

    public Color StartColor
    {
        get => (Color)GetValue(StartColorProperty);
        set => SetValue(StartColorProperty, value);
    }

    public static readonly BindableProperty EndColorProperty =
        BindableProperty.Create(nameof(EndColor), typeof(Color),
                                typeof(CustomFrame), Color.White);

    public Color EndColor
    {
        get => (Color)GetValue(EndColorProperty);
        set => SetValue(EndColorProperty, value);
    }

    public CustomFrame(){}

}

[assembly: ExportRenderer(typeof(CustomFrame), typeof(CustomControls.CustomFrame.CustomFrameRenderer))]
namespace iOS.CustomControls.CustomFrame
{
public class CustomFrameRenderer : FrameRenderer
{
private CGColor startColor;
private CGColor endColor;

    CAGradientLayer gradientLayer = new CAGradientLayer()
    {
        StartPoint = new CGPoint(0, 0.5),
        EndPoint = new CGPoint(1, 0.5)
    };

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

        if (e.NewElement == null) return;

        startColor = ((DPP.CustomControls.CustomFrame)Element).StartColor.ToCGColor();
        endColor = ((DPP.CustomControls.CustomFrame)Element).EndColor.ToCGColor();
    }

    public override void Draw(CGRect rect) //bug with update color
    {
        Element.ForceLayout();

        if (Element == null) return;

        gradientLayer.Frame = rect;
        gradientLayer.Colors = new CGColor[] { startColor, endColor};
        gradientLayer.MasksToBounds = false;

        NativeView.Layer.InsertSublayer(gradientLayer, 0);

        base.Draw(rect);
    }
}

}

In component binding
<c:CustomFrame BackgroundColor=" StartColor="{Binding StartColor}" EndColor="{Binding EndColor}">

on page set itemsource
Items = new ObservableCollection
{
new FrameModel { StartColor = Color.FromHex("#db2e20"), EndColor = Color.Blue },
new FrameModel { StartColor = Color.FromHex("#db2e20"), EndColor = Color.FromHex("#f51d2f")},
new FrameModel { StartColor = Color.FromHex("#db2e20"), EndColor = Color.Blue },
new FrameModel { StartColor = Color.FromHex("#db2e20"), EndColor = Color.FromHex("#f51d2f")},
new FrameModel { StartColor = Color.FromHex("#db2e20"), EndColor = Color.Blue },
new FrameModel { StartColor = Color.FromHex("#db2e20"), EndColor = Color.FromHex("#f51d2f")},
};

listView.ItemsSource = Items;

private async void LoadItems()
{
if (isLoading) return;

        isLoading = true;

        await Task.Delay(4000);

        for (var i = 15; i > 0; i--)
        {
            Items.Add(new FrameModel { StartColor = Color.FromHex("#ff6423"), EndColor = Color.FromHex("#ffb02f") });
            lastItem++;
        }

        isLoading = false;
    }

after load new items, items confused colors, if scroll to start, elements behave as before (without mixing colors)

I will be very grateful for your ideas how you can solve this problem, my attempts to fix it dried out

sorry for bad english


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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