Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HeathHopkins/5017065951c9193706404081930c4f85 to your computer and use it in GitHub Desktop.
Save HeathHopkins/5017065951c9193706404081930c4f85 to your computer and use it in GitHub Desktop.
Prism NavigationPage
<?xml version="1.0" encoding="utf-8" ?>
<NavigationPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
BackgroundColor="{Binding BackgroundColor}"
BarBackgroundColor="{Binding BarBackgroundColor}"
BarTextColor="{Binding BarTextColor}"
Title="{Binding Title}"
x:Class="MyProject.Views.PrismNavigationPage">
</NavigationPage>
using Prism.Navigation;
using Xamarin.Forms;
namespace MyProject.Views
{
public partial class PrismNavigationPage : NavigationPage
{
Page _lastPage;
public PrismNavigationPage()
{
InitializeComponent();
this.Popped += OnPoppedHandler;
this.Pushed += OnPushedHandler;
}
void OnPoppedHandler( object sender, NavigationEventArgs e )
{
var parameters = new NavigationParameters();
if( _lastPage != null )
{
HandleINavigationAware( _lastPage as INavigationAware, parameters, true );
HandleINavigationAware( _lastPage.BindingContext as INavigationAware, parameters, true );
}
HandleINavigationAware( CurrentPage as INavigationAware, parameters, false );
HandleINavigationAware( CurrentPage.BindingContext as INavigationAware, parameters, false );
_lastPage = CurrentPage;
}
void OnPushedHandler( object sender, NavigationEventArgs e )
{
_lastPage = CurrentPage;
}
void HandleINavigationAware( INavigationAware aware, NavigationParameters parameters, bool ExecuteFrom )
{
if (aware == null) return;
if( ExecuteFrom ){
aware.OnNavigatedFrom(parameters);
}
else{
aware.OnNavigatedTo(parameters);
}
}
}
}
using Prism.Navigation;
using Xamarin.Forms;
namespace MyProject.ViewModels
{
public class PrismNavigationPageViewModel : BindableBase, INavigationAware
{
private Color _backgroundColor = Color.White;
public Color BackgroundColor
{
get { return _backgroundColor; }
set { SetProperty( ref _backgroundColor, value ); }
}
private Color _barBackgroundColor = Color.Black;
public Color BarBackgroundColor
{
get { return _barBackgroundColor; }
set { SetProperty( ref _barBackgroundColor, value ); }
}
private Color _barTextColor = Color.White;
public Color BarTextColor
{
get { return _barTextColor; }
set { SetProperty( ref _barTextColor, value ); }
}
private string _title;
public string Title
{
get { return _title; }
set { SetProperty( ref _title, value ); }
}
public void OnNavigatedFrom( NavigationParameters parameters )
{
}
public void OnNavigatedTo( NavigationParameters parameters )
{
if( parameters.ContainsKey( "title" ) )
Title = ( string )parameters[ "title" ];
if( parameters.ContainsKey( "backgroundColor" ) )
BackgroundColor = GetColor( parameters[ "backgroundColor" ] );
if( parameters.ContainsKey( "barBackgroundColor" ) )
BarBackgroundColor = GetColor( parameters[ "barBackgroundColor" ] );
if( parameters.ContainsKey( "barTextColor" ) )
BarTextColor = GetColor( parameters[ "barTextColor" ] );
}
private Color GetColor( object colorParam )
{
var converter = new ColorTypeConverter();
return (Color)converter.ConvertFromInvariantString( colorParam.ToString() );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment