Skip to content

Instantly share code, notes, and snippets.

@skendrot
Created January 16, 2015 04:45
Show Gist options
  • Save skendrot/9ba45cff2b430ccf6feb to your computer and use it in GitHub Desktop.
Save skendrot/9ba45cff2b430ccf6feb to your computer and use it in GitHub Desktop.
// WinRT Implementation of the base Behavior classes
public abstract class Behavior<T> : Behavior where T : DependencyObject
{
protected T AssociatedObject
{
get { return base.AssociatedObject as T; }
}
protected override void OnAttached()
{
base.OnAttached();
if (this.AssociatedObject == null) throw new InvalidOperationException("AssociatedObject is not of the right type");
}
}
public abstract class Behavior : DependencyObject, IBehavior
{
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
OnAttached();
}
public void Detach()
{
OnDetaching();
}
protected virtual void OnAttached()
{
}
protected virtual void OnDetaching()
{
}
protected DependencyObject AssociatedObject { get; set; }
DependencyObject IBehavior.AssociatedObject
{
get { return this.AssociatedObject; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment