Xamarin Forms ProgressBar高度不可控制

时间:2018-11-22 11:16:06

标签: xamarin xamarin.forms android-progressbar

我们已经从Xamarin Forms 2.5升级到3.4。 与以前相比,Android上的ProgressBar变得更厚,并且不受XAML的HeightRequest的控制。

它在网格布局内。

任何想法如何控制高度?

2 个答案:

答案 0 :(得分:0)

您可以通过在xamarin中使用“渲染”来修改进度条的高度。

CustomProgressBarRenderer.cs

using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(App69.CustomProgressBar), typeof(App69.Droid.CustomProgressBarRenderer))]
namespace App69.Droid
{
    public class CustomProgressBarRenderer : ProgressBarRenderer
    {
        public CustomProgressBarRenderer(Context context) : base(context) { }
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.ScaleY = 10; //Changes the height
            }
        }
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App69"
             x:Class="App69.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Label Text="Top Left" Grid.Row="0" Grid.Column="0" />
        <Label Text="Top Right" Grid.Row="0" Grid.Column="1" />
        <Label Text="Bottom Left" Grid.Row="0" Grid.Column="1" />
        <local:CustomProgressBar Progress="0.5"  Grid.Row="1" Grid.Column="1" />
    </Grid>

</ContentPage>

答案 1 :(得分:0)

 class CustomProgressBarRenderer : ProgressBarRenderer
    {
        /// <summary>
        /// Raises the <see cref="E:ElementChanged" /> event.
        /// </summary>
        /// <param name="e">The <see cref="ElementChangedEventArgs{Xamarin.Forms.ProgressBar}"/> instance containing the event data.</param>
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
        {
            base.OnElementChanged(e);
            var element = Element as CustomProgressBar;
            Control.IndeterminateDrawable.SetColorFilter(element.BarColor.ToAndroid(), PorterDuff.Mode.SrcIn);
            Control.ProgressDrawable.SetColorFilter(element.BarColor.ToAndroid(), PorterDuff.Mode.SrcIn);
            Control.ScaleY = element.BarHeight;
        }

        /// <summary>
        /// Called when [element property changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            var element = Element as CustomProgressBar;
            Control.IndeterminateDrawable.SetColorFilter(element.BarColor.ToAndroid(), PorterDuff.Mode.SrcIn);
            Control.ProgressDrawable.SetColorFilter(element.BarColor.ToAndroid(), PorterDuff.Mode.SrcIn);
            Control.ScaleY = element.BarHeight;
        }
    }