Instantly share code, notes, and snippets.
Created
August 24, 2016 19:23
-
Star
1
(1)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save dansiegel/34428019b700ea515e158b8729e2465e to your computer and use it in GitHub Desktop.
Prism NavigationPage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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