Skip to content

Instantly share code, notes, and snippets.

@olexale
Created September 1, 2017 15:04
Show Gist options
  • Select an option

  • Save olexale/b4cfff068eef592049db4e504794a1b5 to your computer and use it in GitHub Desktop.

Select an option

Save olexale/b4cfff068eef592049db4e504794a1b5 to your computer and use it in GitHub Desktop.
namespace BeatlesApp
{
public class TagEffect : RoutingEffect
{
public TagEffect() : base("BeatlesApp.TagEffect") {}
public static readonly BindableProperty TagProperty =
BindableProperty.CreateAttached("Tag", typeof(int), typeof(TagEffect), 0, propertyChanged: OnTagChanged);
public static int GetTag(BindableObject view)
{
return (int)view.GetValue(TagProperty);
}
public static void SetTag(BindableObject view, int value)
{
view.SetValue(TagProperty, value);
}
static void OnTagChanged(BindableObject bindable, object oldValue, object newValue)
{
var view = bindable as View;
if (view == null) { return ; }
var tag = (int)newValue;
if (tag > 0)
{
view.Effects.Add(new TagEffect());
}
else
{
var toRemove = view.Effects.FirstOrDefault(e => e is TagEffect);
if (toRemove != null)
{
view.Effects.Remove(toRemove);
}
}
}
}
}
[assembly: ResolutionGroupName("BeatlesApp")]
[assembly: ExportEffect(typeof(TagEffect), "TagEffect")]
namespace BeatlesApp.iOS
{
public class TagEffect : PlatformEffect
{
private System.nint _tag;
protected override void OnAttached()
{
_tag = Control.Tag;
Control.Tag = BeatlesApp.TagEffect.GetTag(Element);
}
protected override void OnDetached()
{
Control.Tag = _tag;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment