Skip to content

Instantly share code, notes, and snippets.

@matthieugd
Forked from Christopherjl/Behavior.cs
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthieugd/3536efd83706e659e142 to your computer and use it in GitHub Desktop.
Save matthieugd/3536efd83706e659e142 to your computer and use it in GitHub Desktop.
Snippet Name: Android Behavior - WPF Style
Platform(s): Xamarin.Android
Function: Reproduce the beloved WPF behaviors in Android
Snippet:
public abstract class Behavior<T> : View where T:View
{
int _viewId;
protected Behavior(Context context, IAttributeSet attrs)
: base(context, attrs)
{
_viewId = context.ObtainStyledAttributes(attrs, Resource.Styleable.Behavior).GetResourceId(Resource.Styleable.Behavior_View,-1);
}
protected override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
View =RootView.FindViewById<T>(_viewId);
OnAttached();
}
protected override void OnDetachedFromWindow()
{
base.OnDetachedFromWindow();
OnDetached();
}
public abstract void OnAttached();
public abstract void OnDetached();
public T View
{
get;
private set;
}
}
Exemple of Usage: behavior to hide the keyboard when scrolling a listview
<com.mycompany.behaviors.ListViewHideKeyboardOnScroll
android:layout_width="0px"
android:layout_height="0px"
local:View="@+id/resultsList" />
Credit : Chris in-house Rockstar Developer Lamothe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment