xamarin.formsimagebuttonaspect-fit

Xamarin forms - Dynamically-bound image in CarouselView lost Aspect.Fit after image change on Android


I am working on a cross-platform project using Xamarin forms. I am using carouselview to display an image button. See the code snippets below. The customImageButtonStyle sets to Aspect.Aspect.Fit. Initially it works as intended. However, after the image is changed, the image is still displayed, but smaller, ie. no longer does Aspect.Fit. If I place the same code outside the carouselview, it works without such a issue.

    <StackLayout Grid.Row="0" Grid.Column="1" Orientation="Vertical" HorizontalOptions="CenterAndExpand">
                <Grid x:Name="relayGrid" HorizontalOptions="CenterAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="4.0*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <views:CustomImageButton x:Name="relayButton" Source="{Binding RelayIcon}" Grid.Row="0" Grid.Column="0" 
                                             Style="{StaticResource customImageButtonStyle}"
                                             Command="{Binding NavigateCommand}" CommandParameter="{x:Type views:RelayPage}" />
                    <Label Text="RELAYS" Grid.Row="1" Grid.Column="0" Style="{StaticResource deviceHomeBottomTextStyle}" />
                </Grid>
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Command="{Binding NavigateCommand}" CommandParameter="{x:Type views:RelayPage}" />
                </StackLayout.GestureRecognizers>
            </StackLayout>
        </Grid>
    </x:Array>
</controls:CarouselViewControl.ItemsSource>

Please note this issue is also logged at https://github.com/alexrainman/CarouselView/issues/561


Solution

  • It seems this is a Xamarin forms issue. I've tried to replace the image button with an image, the problem goes away. It seems sometimes AspectFit doesn't work for ImageButton when the bound image changes from a view model.