Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
ProgressBar Attached Property to enable progress animation (#352)
Browse files Browse the repository at this point in the history
* ProgressBar Attached Property to enable progress animation

* Fixes remarks on PR

* Fix remarks PR

* Changes need for new gallery viewmodel

* Correct version from before rebase

* Update AttachedPropertiesGalleryViewModel.cs

* Update ProgressBarAttachedProperties.shared.cs

* Update ProgressBarAttachedProperties.shared.cs

* Change Attached property to behavior

* Update samples/XCT.Sample/Xamarin.CommunityToolkit.Sample.csproj

Co-authored-by: Andrei <andrei.misiukevich@gmail.com>

* Update src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/ProgressBarAnimationBehavior.cs

Co-authored-by: Andrei <andrei.misiukevich@gmail.com>

* Update src/CommunityToolkit/Xamarin.CommunityToolkit/Xamarin.CommunityToolkit.csproj

Co-authored-by: Andrei <andrei.misiukevich@gmail.com>

* Update src/CommunityToolkit/Xamarin.CommunityToolkit/Behaviors/ProgressBarAnimationBehavior.cs

Co-authored-by: Cfun <15718354+Cfun1@users.noreply.github.com>

* Update src/CommunityToolkit/Xamarin.CommunityToolkit/AttachedProperties/ProgressBarAttachedProperties.shared.cs

Co-authored-by: Cfun <15718354+Cfun1@users.noreply.github.com>

* Remove old attached property and rename new behavior

Co-authored-by: Gerald Versluis <gerald@verslu.is>
Co-authored-by: Andrei <andrei.misiukevich@gmail.com>
Co-authored-by: Cfun <15718354+Cfun1@users.noreply.github.com>
  • Loading branch information
4 people committed Feb 15, 2021
1 parent 459fc4e commit 0d55a63
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<pages:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Behaviors"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Behaviors.ProgressBarAnimationBehaviorPage">

<pages:BasePage.BindingContext>
<vm:ProgressBarAnimationBehaviorViewModel />
</pages:BasePage.BindingContext>

<StackLayout Padding="{StaticResource ContentPadding}"
Spacing="50"
VerticalOptions="CenterAndExpand">
<ProgressBar>
<ProgressBar.Behaviors>
<xct:ProgressBarAnimationBehavior AnimateProgress="{Binding Progress}" />
</ProgressBar.Behaviors>
</ProgressBar>

<Button Text="Set to 0" Command="{Binding SetTo0Command}" />
<Button Text="Set to 50" Command="{Binding SetTo50Command}" />
<Button Text="Set to 100" Command="{Binding SetTo100Command}" />
</StackLayout>
</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Behaviors
{
public partial class ProgressBarAnimationBehaviorPage : BasePage
{
public ProgressBarAnimationBehaviorPage()
=> InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class BehaviorsGalleryViewModel : BaseGalleryViewModel
typeof(CharactersValidationBehaviorPage),
nameof(CharactersValidationBehavior),
"Changes an Entry's text color when an invalid string is provided."),
new SectionModel(
typeof(ProgressBarAnimationBehaviorPage),
nameof(ProgressBarAnimationBehavior),
"Animate the progress for the ProgressBar")
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Behaviors
{
public class ProgressBarAnimationBehaviorViewModel : BaseViewModel
{
double progress;
ICommand setTo0Command;
ICommand setTo50Command;
ICommand setTo100Command;

public double Progress
{
get => progress;
set
{
progress = value;
OnPropertyChanged();
}
}

public ICommand SetTo0Command => setTo0Command ??= new AsyncCommand(() => SetProgress(0));

public ICommand SetTo50Command => setTo50Command ??= new AsyncCommand(() => SetProgress(0.5));

public ICommand SetTo100Command => setTo100Command ??= new AsyncCommand(() => SetProgress(1));

async Task SetProgress(double
progress)
{
Progress = progress;
}
}
}
1 change: 0 additions & 1 deletion samples/XCT.Sample/Xamarin.CommunityToolkit.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@
<ProjectReference Include="..\..\src\Markup\Xamarin.CommunityToolkit.Markup\Xamarin.CommunityToolkit.Markup.csproj" />
</ItemGroup>
</Project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Xamarin.CommunityToolkit.Behaviors.Internals;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Behaviors
{
public class ProgressBarAnimationBehavior : BaseBehavior<ProgressBar>
{
/// <summary>
/// Backing BindableProperty for the <see cref="AnimateProgress"/> property.
/// </summary>
public static readonly BindableProperty AnimateProgressProperty =
BindableProperty.CreateAttached(nameof(AnimateProgress), typeof(double), typeof(ProgressBar), 0.0d, propertyChanged: OnAnimateProgressPropertyChanged);

public double AnimateProgress
{
get => (double)GetValue(AnimateProgressProperty);
set => SetValue(AnimateProgressProperty, value);
}

static void OnAnimateProgressPropertyChanged(BindableObject bindable, object oldValue, object newValue)
=> ((ProgressBarAnimationBehavior)bindable).Animate();

void Animate()
=> View.ProgressTo(AnimateProgress, 500, Easing.Linear);
}
}

0 comments on commit 0d55a63

Please sign in to comment.