首页 > 解决方案 > 自定义进度条未正确显示

问题描述

我用这个自定义的渲染器改变了我的进度条的高度:

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;
  }
}

我在网格中使用我的自定义进度条。问题是,我看不到自定义进度条。看起来进度条位于另一个控件后面的页面顶部。因为如果我将进度条放在网格中 Margin="50" 的 StackLayout 中,则会出现进度条。然后看起来像

在此处输入图像描述

在 Android 应用程序中一切正常。此问题仅在IOS APP中出现。

有人知道如何将进度条居中吗?

谢谢

Xaml 代码

<customPages:CommonToolbarPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MC.Core.Views.Learn.Simple.LearnSimpleView"
         xmlns:viewModelBase="clr-namespace:MC.Core.ViewModels.Base;assembly=MC.Core"
         xmlns:customPages="clr-namespace:MC.Core.CustomPage;assembly=MC.Core"
         xmlns:control="clr-namespace:MC.Core.Controls;assembly=MC.Core"
         xmlns:translator="clr-namespace:MC.Core.Helpers"
         viewModelBase:ViewModelLocator.AutoWireViewModel="true"
         Title="{Binding PageTitle}">
<ContentPage.ToolbarItems>
    <ToolbarItem Command="{Binding RestartLearnCommand}" Text="{translator:Translate ButtonReset}" Priority="0" Order="Primary"></ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.Content>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <BoxView Grid.Row="0" HeightRequest="1" Style="{DynamicResource GreySeparatorLine}"></BoxView>
        <Grid Grid.Row="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
            <!--Start ProgressBar Control-->
            <ContentView Grid.Row="0" Grid.Column="0" Margin="10,0" >
                <Grid Padding="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid Grid.Row="0" Grid.Column="0" Margin="1,0">
                        <Label Style="{DynamicResource RightVsWrongLabelStyle}" Text="{Binding CorrectVsWrongStatus}"></Label>
                        <Label Style="{DynamicResource LearnSystemLabelStyle}" Text="{translator:Translate LearnModusSimple}" ></Label>
                    </Grid>
                    <Grid Grid.Row="1" Grid.Column="0" Margin="1,5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0" Grid.Column="0">
                                <StackLayout  Margin="50">
                                    <control:CustomProgressBar  HeightRequest="10" x:Name="progressBar" Progress="{Binding LearnProgressValue}"></control:CustomProgressBar>
                                    <Label Style="{DynamicResource ProgressBarLabelStyle}" Text="{Binding LabelProgressStatus}"></Label>
                                </StackLayout>
                        </Grid>
                    </Grid>
                </Grid>
                <ContentView.Triggers>
                    <DataTrigger TargetType="ContentView" Binding="{Binding ShowProgressBar}" Value="false">
                        <Setter Property="IsVisible" Value="false"></Setter>
                    </DataTrigger>
                    <DataTrigger TargetType="ContentView" Binding="{Binding ShowProgressBar}" Value="true">
                        <Setter Property="IsVisible" Value="true"></Setter>
                    </DataTrigger>
                </ContentView.Triggers>
            </ContentView>
            <!--End ProgressBar Control-->
            <!-- ... Some other code ... -->
        </Grid>
    </Grid>
</ContentPage.Content>

标签: xamarinxamarin.formsxamarin.ios

解决方案


推荐阅读