Last active
May 13, 2020 08:30
-
-
Save foxanna/4b60983373dfba31d9b915f11f5c623f to your computer and use it in GitHub Desktop.
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.Linq; | |
using Xamarin.Forms; | |
namespace <project>.Effects | |
{ | |
public class RoundCornersEffect : RoutingEffect | |
{ | |
public RoundCornersEffect() : base($"<project>.{nameof(RoundCornersEffect)}") | |
{ | |
} | |
public static readonly BindableProperty CornerRadiusProperty = | |
BindableProperty.CreateAttached( | |
"CornerRadius", | |
typeof(int), | |
typeof(RoundCornersEffect), | |
0, | |
propertyChanged: OnCornerRadiusChanged); | |
public static int GetCornerRadius(BindableObject view) => | |
(int) view.GetValue(CornerRadiusProperty); | |
public static void SetCornerRadius(BindableObject view, int value) => | |
view.SetValue(CornerRadiusProperty, value); | |
private static void OnCornerRadiusChanged(BindableObject bindable, object oldValue, object newValue) | |
{ | |
if (!(bindable is View view)) | |
return; | |
var cornerRadius = (int) newValue; | |
var effect = view.Effects.OfType<RoundCornersEffect>().FirstOrDefault(); | |
if (cornerRadius > 0 && effect == null) | |
view.Effects.Add(new RoundCornersEffect()); | |
if (cornerRadius == 0 && effect != null) | |
view.Effects.Remove(effect); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment