CarouselView Validation

Ángel Rubén Valdeolmos Carmona 586 Reputation points
2021-03-03T20:15:56.94+00:00

I have a CarouselView with multiple ContenViews as if it were a "step by step" form. I wonder how I could validate the content before the user swipes. Do I have to do everything in the CurrentItemChangedCommand?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ángel Rubén Valdeolmos Carmona 586 Reputation points
    2021-05-26T18:12:27.51+00:00

    I have:

    <CarouselView x:Name="CarouselRegister" Position="{Binding Position}" IndicatorView="indicatorView" 
                      Loop="False" 
                      IsSwipeEnabled="False">
            <CarouselView.ItemsSource>
                <x:Array Type="{x:Type ContentView}">
                    <views:CenterView />
                    <views:PersonalInformationView />
                    <views:AddressView />
                </x:Array>
            </CarouselView.ItemsSource>
            <CarouselView.ItemTemplate>
                <DataTemplate x:DataType="d:ContentView">
                    <ContentView Content="{Binding .}" />
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
        <StackLayout Orientation="Horizontal">
            <Button Text="{xct:Translate Back}" BackgroundColor="DarkOrange" Command="{Binding BackCommand}">
                <Button.Triggers>
                    <DataTrigger TargetType="Button" Binding="{Binding Position}" Value="0">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Button.Triggers>
            </Button>
            <IndicatorView x:Name="indicatorView"
                           IndicatorSize="14"
                           IndicatorsShape="Circle"
                           Padding="14"
                           IndicatorColor="LightGray"
                           SelectedIndicatorColor="{StaticResource Primary}"
                           HorizontalOptions="CenterAndExpand">
            </IndicatorView>
            <Button Text="{xct:Translate Next}" BackgroundColor="DarkGreen" Command="{Binding NextCommand}" />
        </StackLayout>
    

    And:

     public ICommand NextCommand => new Command(Next);
    
        private void Next()
        {
            if (Position + 1 != 0)
            {
                Position++;
            }
        }
    
        public ICommand BackCommand => new Command(Back);
    
        private void Back()
        {
            if (Position != 0)
            {
                Position--;
            }
        }
    

    In each ContentView, I must do a validation through Behavior with XamarinCommunityToolkit, but I don't know very well how to approach it, since each Behavior is independent in ContentView, any ideas with my example?

    0 comments No comments