Skip to content

Instantly share code, notes, and snippets.

@rmenezes
Forked from dansiegel/BehaviorBase{T}.cs
Created January 13, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmenezes/b6310f9c6a66d1b106cebfb9ccf15ed2 to your computer and use it in GitHub Desktop.
Save rmenezes/b6310f9c6a66d1b106cebfb9ccf15ed2 to your computer and use it in GitHub Desktop.
Prism Tabbed Navigation
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
public T AssociatedObject { get; private set; }
protected override void OnAttachedTo( T bindable )
{
base.OnAttachedTo( bindable );
AssociatedObject = bindable;
if( bindable.BindingContext != null )
{
BindingContext = bindable.BindingContext;
}
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom( T bindable )
{
base.OnDetachingFrom( bindable );
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
void OnBindingContextChanged( object sender, EventArgs e )
{
OnBindingContextChanged();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
}
public interface IMultiPageNavigationAware
{
void OnInternalNavigatedFrom( NavigationParameters navParams );
void OnInternalNavigatedTo( NavigationParameters navParams );
}
public class MultiPageNavigationBehavior : BehaviorBase<MultiPage<Page>>
{
private Page _lastSelectedPage;
protected override void OnAttachedTo( MultiPage<Page> bindable )
{
base.OnAttachedTo( bindable );
bindable.CurrentPageChanged += CurrentPageChangedHandler;
}
protected override void OnDetachingFrom( MultiPage<Page> bindable )
{
base.OnDetachingFrom( bindable );
bindable.CurrentPageChanged -= CurrentPageChangedHandler;
}
private void CurrentPageChangedHandler( object sender, EventArgs args )
{
NavigationParameters parameters = new NavigationParameters();
var lastPageContextAware = _lastSelectedPage?.BindingContext as IMultiPageNavigationAware;
lastPageContextAware?.OnInternalNavigatedFrom( parameters );
var newPageContextAware = AssociatedObject.CurrentPage.BindingContext as IMultiPageNavigationAware;
newPageContextAware?.OnInternalNavigatedTo( parameters );
_lastSelectedPage = AssociatedObject.CurrentPage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment