如何在Xamarin Forms 3.3上设置ProgressBar的高度?

时间:2018-11-19 15:59:21

标签: xamarin xamarin.forms xamarin.ios

我正在使用Xamarin Forms 3.3,我想更改ProgressBar的高度。我执行它: How to change height of progress bar in Xamarin Forms? 但是我不能更改 ProgressBar 的高度。这是我的代码:

1)类自定义控件ProgressBar:

public class ColorProgressBar : ProgressBar
{


public static BindableProperty BarColorProperty = BindableProperty.Create(
            nameof(BarColor), typeof(Color), typeof(ColorProgressBar), default(Color));

    public static BindableProperty ProgressTintColorProperty = BindableProperty.Create(
            nameof(ProgressTintColor), typeof(Color), typeof(ColorProgressBar), default(Color));

    public static BindableProperty TrackTintColorProperty = BindableProperty.Create(
            nameof(TrackTintColor), typeof(Color), typeof(ColorProgressBar), default(Color));

    public Color BarColor
    {
        get => (Color)GetValue(BarColorProperty);
        set => SetValue(BarColorProperty, value);
    }

    public Color ProgressTintColor
    {
        get => (Color)GetValue(ProgressTintColorProperty);
        set => SetValue(ProgressTintColorProperty, value);
    }

    public Color TrackTintColor
    {
        get => (Color)GetValue(TrackTintColorProperty);
        set => SetValue(TrackTintColorProperty, value);
    }

    public float HeightOfProgressBar { get; set; }
    }

2)对于iOS:

public class ColorProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
        => iOS.Control.Logger.InvokeAction(() =>
    {
        base.OnElementChanged(e);
        if (e.NewElement == null)
            return;

        if (Control != null)
        {
            UpdateBarColor();
        }
    });

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        => iOS.Control.Logger.InvokeAction(() =>
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == ColorProgressBar.BarColorProperty.PropertyName)
        {
            UpdateBarColor();
        }
    });

    private void UpdateBarColor() => iOS.Control.Logger.InvokeAction(() =>
    {
        var element = (ColorProgressBar) Element;
        if (element == null)
            return;

        Control.TintColor = element.BarColor.ToUIColor();
        Control.ProgressTintColor = element.ProgressTintColor.ToUIColor();
        Control.TrackTintColor = element.TrackTintColor.ToUIColor();
    });

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        var element = (ColorProgressBar)Element;
        if (element == null)
            return;

        var X = 1.0f;
        var Y = element.HeightOfProgressBar > 0.0f ? element.HeightOfProgressBar : 1.0f;

        var transform = CGAffineTransform.MakeScale(X, Y);
        Transform = transform;
    }
}

然后查看.xaml:

<control:ColorProgressBar Grid.Row="1"
                                  HeightOfProgressBar="8.0"
                                  TrackTintColor="{x:Static color:BasePalette.DarkestColor}"
                                  ProgressTintColor="{x:Static color:NeutralPalette.RedColor}"
                                  Progress="{Binding PercentDataValue}">
</control:ColorProgressBar>

我在运行iOS 12.0的设备上进行了测试。

谢谢!

1 个答案:

答案 0 :(得分:0)

您错过了对链接的SO线程的评论:

  

要使iOS渲染器正常工作,我需要更改此设置。到Control.Transform = transform;

因此,将Transform = transform;的{​​{1}}方法中的LayoutSubviews更改为ColorProgressBarRenderer可以使您的示例按预期工作,即进度条的高度已更改:

Control.Transform = transform;