Skip to content

Instantly share code, notes, and snippets.

@PureWeen
Created April 19, 2018 02:03
Show Gist options
  • Save PureWeen/0a3c6271b4f6691579b223975a689eed to your computer and use it in GitHub Desktop.
Save PureWeen/0a3c6271b4f6691579b223975a689eed to your computer and use it in GitHub Desktop.
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Controls.Effects
{
[Preserve(AllMembers = true)]
public class AttachedStateEffect : RoutingEffect
{
public enum AttachedState
{
Unknown,
Attached,
Detached
}
public AttachedState State { get; set; } = AttachedState.Unknown;
public const string EffectName = "AttachedStateEffect";
public AttachedStateEffect() : base($"{Issues.Effects.ResolutionGroupName}.{EffectName}")
{
}
}
}
using System;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.ControlGallery.iOS;
using Xamarin.Forms.Controls.Effects;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportEffect(typeof(AttachedStateEffectRenderer), AttachedStateEffect.EffectName)]
namespace Xamarin.Forms.ControlGallery.iOS
{
public class AttachedStateEffectRenderer : PlatformEffect
{
AttachedStateEffect Effect;
protected override void OnAttached()
{
Effect = (AttachedStateEffect)Element.Effects.First(e => e is AttachedStateEffect);
if(Effect.State != AttachedStateEffect.AttachedState.Unknown)
{
throw new InvalidOperationException($"Invalid State: {Effect.State} expected {AttachedStateEffect.AttachedState.Unknown}");
}
Effect.State = AttachedStateEffect.AttachedState.Attached;
}
protected override void OnDetached()
{
if (Effect.State != AttachedStateEffect.AttachedState.Attached)
{
throw new InvalidOperationException($"Invalid State: {Effect.State} expected {AttachedStateEffect.AttachedState.Attached}");
}
Effect.State = AttachedStateEffect.AttachedState.Detached;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment