Skip to content

Commit

Permalink
make NavigationExtension bindable
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Feb 20, 2019
1 parent 336a405 commit dec906e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
11 changes: 9 additions & 2 deletions Source/Xamarin/Prism.Forms/Navigation/Xaml/GoBackExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ namespace Prism.Navigation.Xaml
[ContentProperty(nameof(GoBackType))]
public class GoBackExtension : NavigationExtensionBase
{
public GoBackType GoBackType { get; set; } = GoBackType.Default;
public static readonly BindableProperty GoBackTypeProperty =
BindableProperty.Create(nameof(GoBackType), typeof(GoBackType), typeof(GoBackExtension), GoBackType.Default);

public GoBackType GoBackType
{
get => (GoBackType)GetValue(GoBackTypeProperty);
set => SetValue(GoBackTypeProperty, value);
}

protected override async Task HandleNavigation(INavigationParameters parameters, INavigationService navigationService)
{
Expand All @@ -16,7 +23,7 @@ await navigationService.GoBackToRootAsync(parameters) :

if (result.Exception != null)
{
Log(result.Exception);
Log(result.Exception, parameters);
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions Source/Xamarin/Prism.Forms/Navigation/Xaml/NavigateToExtension.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Prism.Navigation.Xaml
{
[ContentProperty(nameof(Name))]
public class NavigateToExtension : NavigationExtensionBase
{
public string Name { get; set; }
public static readonly BindableProperty NameProperty =
BindableProperty.Create(nameof(Name), typeof(string), typeof(NavigateToExtension), null);

public string Name
{
get => (string)GetValue(NameProperty);
set => SetValue(NameProperty, value);
}

protected override async Task HandleNavigation(INavigationParameters parameters, INavigationService navigationService)
{
var result = await navigationService.NavigateAsync(Name, parameters, animated: Animated, useModalNavigation: UseModalNavigation);
if(result.Exception != null)
{
Log(result.Exception);
Log(result.Exception, parameters);
}
}

protected override void Log(Exception ex, INavigationParameters parameters)
{
Xamarin.Forms.Internals.Log.Warning("Warning", $"{GetType().Name} threw an exception while navigating to '{Name}'.");
Xamarin.Forms.Internals.Log.Warning("Exception", ex.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@

namespace Prism.Navigation.Xaml
{
public abstract class NavigationExtensionBase : IMarkupExtension<ICommand>, ICommand
public abstract class NavigationExtensionBase : BindableObject, IMarkupExtension<ICommand>, ICommand
{
public static readonly BindableProperty AnimatedProperty =
BindableProperty.Create(nameof(Animated), typeof(bool), typeof(NavigationExtensionBase), true);

public static readonly BindableProperty UseModalNavigationProperty =
BindableProperty.Create(nameof(UseModalNavigation), typeof(bool?), typeof(NavigationExtensionBase), null);

private IServiceProvider ServiceProvider;

private Element _targetElement;
Expand Down Expand Up @@ -44,9 +50,17 @@ protected internal get
set => _sourcePage = value;
}

public bool Animated { get; set; } = true;
public bool Animated
{
get => (bool)GetValue(AnimatedProperty);
set => SetValue(AnimatedProperty, value);
}

public bool? UseModalNavigation { get; set; }
public bool? UseModalNavigation
{
get => (bool?)GetValue(UseModalNavigationProperty);
set => SetValue(UseModalNavigationProperty, value);
}

public bool CanExecute(object parameter) => !IsNavigating;

Expand All @@ -66,7 +80,7 @@ public async void Execute(object parameter)
}
catch(Exception ex)
{
Log(ex);
Log(ex, parameters);
}
finally
{
Expand All @@ -86,7 +100,7 @@ public ICommand ProvideValue(IServiceProvider serviceProvider)
return this;
}

protected virtual void Log(Exception ex)
protected virtual void Log(Exception ex, INavigationParameters parameters)
{
Xamarin.Forms.Internals.Log.Warning("Warning", $"{GetType().Name} threw an exception");
Xamarin.Forms.Internals.Log.Warning("Exception", ex.ToString());
Expand Down

0 comments on commit dec906e

Please sign in to comment.