本文介绍了如何更改 Xamarin Forms 中进度条的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在代码中更改 Xamarin Forms ProgressBar 的高度?我正在使用 Xamarin Forms V2.1.

How do I change the height of a Xamarin Forms ProgressBar in code? Am using Xamarin Forms V2.1.

.HeightRequest.MinimumHeightRequest 似乎没有效果.默认进度条非常细,甚至可能不会被注意到.

.HeightRequest and .MinimumHeightRequest seem to have no effect. The default progress bar is so thin that it might not even be noticed.

.BackgroundColor 似乎也不起作用.

我在这里错过了什么?

推荐答案

为此您需要自定义渲染器:

You need custom renderers for this:

首先为您的自定义进度条创建一个类:

First create a class for your custom progress bar:

public class CustomProgressBar :ProgressBar
{
  public CustomProgressBar()
   {
   }
}

然后还为您的自定义进度条渲染器添加一个新文件:

And then also add a new file for your custom progress bar renderer:

对于 iOS:

public class CustomProgressBarRenderer : ProgressBarRenderer
{
    protected override void OnElementChanged(
     ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);

        Control.ProgressTintColor = Color.FromRgb(182, 231, 233).ToUIColor();// This changes the color of the progress
    }


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

        var X = 1.0f;
        var Y = 10.0f; // This changes the height

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

安卓:

public class CustomProgressBarRenderer :ProgressBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
    {
        base.OnElementChanged(e);


        Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid()); //Change the color
        Control.ScaleY = 10; //Changes the height

    }
}

希望对你有帮助!

这篇关于如何更改 Xamarin Forms 中进度条的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:04