Skip to content

Instantly share code, notes, and snippets.

@foxanna
Last active May 13, 2020 08:30
Show Gist options
  • Save foxanna/4b60983373dfba31d9b915f11f5c623f to your computer and use it in GitHub Desktop.
Save foxanna/4b60983373dfba31d9b915f11f5c623f to your computer and use it in GitHub Desktop.
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