Last active
October 3, 2018 07:32
-
-
Save lubiepomaranczki/b02db205021bf1993a72d8990516d665 to your computer and use it in GitHub Desktop.
Taken from sample repository: https://github.com/lubiepomaranczki/CustomLoader associated with my article:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Xamarin.Forms; | |
namespace CustomLoader | |
{ | |
public class CustomLoader : Image | |
{ | |
#region Fields | |
private CancellationTokenSource cancellationToken; | |
#endregion | |
#region Binadables | |
public static BindableProperty IsRunningProperty = BindableProperty.Create( | |
propertyName: nameof(IsRunning), | |
returnType: typeof(bool), | |
declaringType: typeof(CustomLoader), | |
defaultValue: false); | |
public bool IsRunning | |
{ | |
get { return (bool)GetValue(IsRunningProperty); } | |
set { SetValue(IsRunningProperty, value); } | |
} | |
public static BindableProperty RotationLenghtProperty = BindableProperty.Create( | |
propertyName: nameof(RotationLenght), | |
returnType: typeof(int), | |
declaringType: typeof(CustomLoader), | |
defaultValue: 2500); | |
public int RotationLenght | |
{ | |
get { return (int)GetValue(RotationLenghtProperty); } | |
set { SetValue(RotationLenghtProperty, value); } | |
} | |
public static BindableProperty EasingProperty = BindableProperty.Create( | |
propertyName: nameof(Easing), | |
returnType: typeof(Easing), | |
declaringType: typeof(CustomLoader), | |
defaultValue: Easing.CubicInOut); | |
public Easing Easing | |
{ | |
get { return (Easing)GetValue(EasingProperty); } | |
set { SetValue(EasingProperty, value); } | |
} | |
#endregion | |
#region Constructor(s) | |
public CustomLoader() | |
{ | |
Opacity = 0; | |
} | |
#endregion | |
#region Overrides | |
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
{ | |
base.OnPropertyChanged(propertyName); | |
if (propertyName == IsRunningProperty.PropertyName) | |
{ | |
if (IsRunning) | |
{ | |
this.FadeTo(1); | |
cancellationToken = new CancellationTokenSource(); | |
RotateElement(this, cancellationToken.Token); | |
} | |
else | |
{ | |
cancellationToken?.Cancel(); | |
this.FadeTo(0); | |
} | |
} | |
} | |
#endregion | |
#region Methods | |
private async Task RotateElement(VisualElement element, CancellationToken cancellation) | |
{ | |
while (!cancellation.IsCancellationRequested) | |
{ | |
await element.RotateTo(360, (uint)RotationLenght, this.Easing); | |
await element.RotateTo(0, 0); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment