45
loading...
This website collects cookies to deliver better user experience
public static readonly BindableProperty PageCountProperty = BindableProperty.Create(nameof(PageCount), typeof(int), typeof(PaginationView), defaultValue: 1, propertyChanged: PageCountPropertyChanged);
public static readonly BindableProperty DisabledColorProperty = BindableProperty.Create(nameof(DisabledColor), typeof(Color), typeof(PaginationView), defaultValue: Color.Gray, propertyChanged: OnDisabledColorPropertyChanged);
public static readonly BindableProperty IconBackgroundColorProperty = BindableProperty.Create(nameof(IconBackgroundColor), typeof(Color), typeof(PaginationView), defaultValue: Color.Green, propertyChanged: IconBackgroundColorPropertyChanged);
public static readonly BindableProperty OnPaginatedProperty = BindableProperty.Create(nameof(OnPaginated), typeof(IAsyncCommand<int>), typeof(PaginationView));
private static void OnDisabledColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((PaginationView)bindable).SetPageNavigationValues();
}
private static void IconBackgroundColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((PaginationView)bindable).SetPageNavigationValues();
}
private static void PageCountPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((PaginationView)bindable).SetPageNavigationValues();
}
private static void CurrentPagePropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((PaginationView)bindable).SetPageNavigationValues();
}
public Color DisabledColor
{
get => (Color)GetValue(DisabledColorProperty);
set => SetValue(DisabledColorProperty, value);
}
public Color IconBackgroundColor
{
get => (Color)GetValue(IconBackgroundColorProperty);
set => SetValue(IconBackgroundColorProperty, value);
}
public int CurrentPage
{
get => (int)GetValue(CurrentPageProperty);
set => SetValue(CurrentPageProperty, value);
}
public int PageCount
{
get => (int)GetValue(PageCountProperty);
set => SetValue(PageCountProperty, value);
}
public IAsyncCommand<int> OnPaginated
{
get => (IAsyncCommand<int>)GetValue(OnPaginatedProperty);
set => SetValue(OnPaginatedProperty, value);
}```
Backing field must have its name same as its bindable property without 'Property' text in it.
private async ValueTask GetLastPageData()
{
if (CurrentPage == PageCount)
return;
CurrentPage = PageCount;
SetPageNavigationValues();
await ExecuteCommand();
}
private async ValueTask GetNextPageData()
{
if (CurrentPage == PageCount)
return;
CurrentPage += 1;
SetPageNavigationValues();
await ExecuteCommand();
}
private async ValueTask GetPreviousPageData()
{
if (CurrentPage == 1)
return;
CurrentPage -= 1;
SetPageNavigationValues();
await ExecuteCommand();
}
private async ValueTask GetFirstPageData()
{
if (CurrentPage == 1)
return;
CurrentPage = 1;
SetPageNavigationValues();
await ExecuteCommand();
}
private async Task ExecuteCommand()
{
if (OnPaginated != null)
await OnPaginated.ExecuteAsync(CurrentPage);
}
private bool _allowPreviousPageNavigation;
public bool AllowPreviousPageNavigation
{
get => _allowPreviousPageNavigation;
set
{
_allowPreviousPageNavigation = value;
PreviousPageButtonForegroundColor = value ? IconBackgroundColor : DisabledColor;
OnPropertyChanged(nameof(AllowPreviousPageNavigation));
}
}
private bool _allowFirstPageNavigation;
public bool AllowFirstPageNavigation
{
get => _allowFirstPageNavigation;
set
{
_allowFirstPageNavigation = value;
FirstPageButtonForegroundColor = value ? IconBackgroundColor : DisabledColor;
OnPropertyChanged(nameof(AllowFirstPageNavigation));
}
}
private bool _allowNextPageNavigation;
public bool AllowNextPageNavigation
{
get => _allowNextPageNavigation;
set
{
_allowNextPageNavigation = value;
NextPageButtonForegroundColor = value ? IconBackgroundColor : DisabledColor;
OnPropertyChanged(nameof(AllowNextPageNavigation));
}
}
private bool _allowLastPageNavigation;
public bool AllowLastPageNavigation
{
get => _allowLastPageNavigation;
set
{
_allowLastPageNavigation = value;
LastPageButtonForegroundColor = value ? IconBackgroundColor : DisabledColor;
OnPropertyChanged(nameof(AllowLastPageNavigation));
}
}
private void SetPageNavigationValues()
{
AllowFirstPageNavigation = true;
AllowNextPageNavigation = true;
AllowLastPageNavigation = true;
AllowPreviousPageNavigation = true;
if (CurrentPage == 1)
{
AllowPreviousPageNavigation = false;
AllowFirstPageNavigation = false;
}
if (CurrentPage == PageCount)
{
AllowNextPageNavigation = false;
AllowLastPageNavigation = false;
}
}
public IAsyncValueCommand MoveToFirstPageCommand { get; set; }
public IAsyncValueCommand MoveToPreviousPageCommand { get; set; }
public IAsyncValueCommand MoveToNextPageCommand { get; set; }
public IAsyncValueCommand MoveToLastPageCommand { get; set; }
public PaginationView()
{
InitializeComponent();
MoveToFirstPageCommand = new AsyncValueCommand(() => GetFirstPageData(), allowsMultipleExecutions: false);
MoveToPreviousPageCommand = new AsyncValueCommand(() => GetPreviousPageData(), allowsMultipleExecutions: false);
MoveToNextPageCommand = new AsyncValueCommand(() => GetNextPageData(), allowsMultipleExecutions: false);
MoveToLastPageCommand = new AsyncValueCommand(() => GetLastPageData(), allowsMultipleExecutions: false);
BindingContext = this;
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Xam.Views.Pagination.PaginationView"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" Margin="0,0,0,10">
<ImageButton HeightRequest="40" BackgroundColor="Transparent" IsEnabled="{Binding AllowFirstPageNavigation}">
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesomeSolid"
Color="{Binding FirstPageButtonForegroundColor}"
Glyph=""/>
</ImageButton.Source>
<ImageButton.Behaviors>
<xct:EventToCommandBehavior EventName="Clicked" Command="{Binding MoveToFirstPageCommand}"></xct:EventToCommandBehavior>
</ImageButton.Behaviors>
</ImageButton>
<ImageButton HeightRequest="40" BackgroundColor="Transparent" IsEnabled="{Binding AllowPreviousPageNavigation}">
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesomeSolid"
Color="{Binding PreviousPageButtonForegroundColor}"
Glyph=""/>
</ImageButton.Source>
<ImageButton.Behaviors>
<xct:EventToCommandBehavior EventName="Clicked" Command="{Binding MoveToPreviousPageCommand}"></xct:EventToCommandBehavior>
</ImageButton.Behaviors>
</ImageButton>
<Frame CornerRadius="20" HorizontalOptions="Start" WidthRequest="30" VerticalOptions="Center" Margin="0" Padding="10" BackgroundColor="{Binding IconBackgroundColor}">
<Label Text="{Binding CurrentPage}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontAttributes="Bold" />
</Frame>
<Label Text="{Binding PageCount,StringFormat='Of {0}'}" VerticalOptions="Center" FontAttributes="Bold"></Label>
<ImageButton HeightRequest="40" BackgroundColor="Transparent" IsEnabled="{Binding AllowNextPageNavigation}">
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesomeSolid"
Color="{Binding NextPageButtonForegroundColor}"
Glyph=""/>
</ImageButton.Source>
<ImageButton.Behaviors>
<xct:EventToCommandBehavior EventName="Clicked" Command="{Binding MoveToNextPageCommand}"></xct:EventToCommandBehavior>
</ImageButton.Behaviors>
</ImageButton>
<ImageButton HeightRequest="40" BackgroundColor="Transparent" IsEnabled="{Binding AllowLastPageNavigation}">
<ImageButton.Source>
<FontImageSource FontFamily="FontAwesomeSolid"
Color="{Binding LastPageButtonForegroundColor}"
Glyph=""/>
</ImageButton.Source>
<ImageButton.Behaviors>
<xct:EventToCommandBehavior EventName="Clicked" Command="{Binding MoveToLastPageCommand}"></xct:EventToCommandBehavior>
</ImageButton.Behaviors>
</ImageButton>
</StackLayout>
Add xmlns in xaml and give it a name.
xmlns:customViews="clr-namespace:Xam.Views.Pagination"
x:Name="Page"
Use the component wherever required, in the page
<customViews:PaginationView
CurrentPage="{Binding Source={x:Reference Page}, Path=BindingContext.PageNumberBackingField}"
PageCount="{Binding Source={x:Reference Page}, Path=BindingContext.PageCountBackingField}"
OnPaginated="{Binding Source={x:Reference Page}, Path=BindingContext.OnPaginatedCommand}"
IconBackgroundColor="Red"
DisabledColor="Gray"/>
45