Xamarin 自定义进度条渲染器问题

Xamarin Custom Progressbar renderer problem

我创建了一个似乎可以工作的新自定义渲染器,但是当我向上或向下滚动时,设置会发生一些奇怪的事情。正如您在照片中看到的,ProgressBar 的大小发生了变化。

它只发生在 iOS。在 Android 上一切正常。 也许有人可以提供帮助?

using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using ExpensesApp.iOS.CustomRenderers;
using CoreGraphics;
using CoreAnimation;
using UIKit;
using System.ComponentModel;
using System.Threading.Tasks;

[assembly: ExportRenderer(typeof(ProgressBar),typeof(CustomProgressBarRenderer))]
namespace ExpensesApp.iOS.CustomRenderers
{
public class CustomProgressBarRenderer : ProgressBarRenderer
{
    public CustomProgressBarRenderer()
    {
        this.ElementChanged += CustomProgressBarRenderer_ElementChanged;
    }

    private void CustomProgressBarRenderer_ElementChanged(object sender, 
    ElementChangedEventArgs<ProgressBar> e)
    {
        setColor(e.NewElement);
    }

    protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
    {
        base.OnElementChanged(e);
        LayoutSubviews();
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        float x = 1.0f;
        float y = 4.0f;
        CGAffineTransform transform = CGAffineTransform.MakeScale(x, y);
        Transform = transform;
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
        if (e.PropertyName == "Progress")
        {
            setColor(sender as ProgressBar);
        }
    }

    void setColor(ProgressBar element)
    {
        try
        {
            if (element != null)
            {
                var progress = element.Progress * 100;
                if (progress <= 20)
                {
                    this.TintColor = UIColor.Green;
                }
                else if (progress > 20 && progress <= 50)
                {
                    this.TintColor = UIColor.Yellow;
                }
                else if (progress > 50 && progress <= 80)
                {
                    this.TintColor = UIColor.Orange;
                }
                else
                {
                    this.TintColor = UIColor.Red;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}
}

将转换设置为控件:

public override void LayoutSubviews()
{
    base.LayoutSubviews();
    float x = 1.0f;
    float y = 4.0f;
    CGAffineTransform transform = CGAffineTransform.MakeScale(x, y);
    Control.Transform = transform;
}

并且不要在 OnElementChanged 中调用 LayoutSubviews();:

protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
{
    base.OnElementChanged(e);
    //LayoutSubviews();
}

谢谢。我试过了,但没有用。我找到了另一个解决方案。 我不知道这是怎么回事。但它按照我想要的方式工作。我还在学习:)

 public CustomProgressBarRenderer()
    {
        this.ElementChanged += CustomProgressBarRenderer_ElementChanged;
    }

    private void CustomProgressBarRenderer_ElementChanged(object sender, ElementChangedEventArgs<ProgressBar> e)
    {
        if (e.NewElement != null)
        {
            e.NewElement.ScaleY = 4;
        }
    }