Skip to content

Commit

Permalink
adding SelectTabExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Jun 9, 2019
1 parent 3856412 commit f4ff482
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Sandbox/Xamarin/HelloWorld/ModuleA/Views/ViewA.xaml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ModuleA.Views.ViewA"
Padding="{OnPlatform iOS='0,20,0,0'}"
Expand All @@ -23,6 +23,8 @@
<Label Text="{Binding Message}" />
<Label Text="{Binding IsActive, StringFormat='IsActive: {0}'}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
<Button Text="Select Tab C"
Command="{prism:SelectTab ViewC}" />
</StackLayout>

</ContentPage>
10 changes: 4 additions & 6 deletions Sandbox/Xamarin/HelloWorld/ModuleA/Views/ViewB.xaml
Expand Up @@ -2,22 +2,20 @@
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ModuleA.Views"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ModuleA.Views.ViewB"
x:Name="viewB"
Padding="{OnPlatform iOS='0,20,0,0'}"
Title="View B">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
</OnPlatform>
</ContentPage.Padding>

<!--<local:PartialViewB prism:ViewModelLocator.AutowirePartialView="{x:Reference viewB}" />-->
<StackLayout>
<Label Text="{Binding Title}" VerticalOptions="Center" HorizontalOptions="Center" />
<Label Text="{Binding IsActive}" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
<Button Text="Select Tab C"
Command="{prism:SelectTab ViewC}" />
</StackLayout>

</ContentPage>
10 changes: 4 additions & 6 deletions Sandbox/Xamarin/HelloWorld/ModuleA/Views/ViewC.xaml
@@ -1,17 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="ModuleA.Views.ViewC"
Padding="{OnPlatform iOS='0,20,0,0'}"
Title="View C">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,20,0,0" />
</OnPlatform>
</ContentPage.Padding>
<StackLayout>
<Label Text="{Binding Title}" VerticalOptions="Center" HorizontalOptions="Center" />
<Button Command="{Binding NavigateCommand}" Text="Navigate" />
</StackLayout>
<Button Text="Select Tab A"
Command="{prism:SelectTab ViewA}" />
</ContentPage>
Expand Up @@ -14,7 +14,7 @@ public abstract class NavigationExtensionBase : Prism.Xaml.ParentPageAwareExtens
public static readonly BindableProperty UseModalNavigationProperty =
BindableProperty.Create(nameof(UseModalNavigation), typeof(bool?), typeof(NavigationExtensionBase), null);

protected internal bool IsNavigating;
protected internal bool IsNavigating { get; private set; }

public bool Animated
{
Expand Down
73 changes: 73 additions & 0 deletions Source/Xamarin/Prism.Forms/Navigation/Xaml/SelectTabExtension.cs
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Prism.Navigation.TabbedPages;
using Prism.Xaml;
using Xamarin.Forms;

namespace Prism.Navigation.Xaml
{
[ContentProperty(nameof(Name))]
public class SelectTabExtension : ParentPageAwareExtension<ICommand>, ICommand
{
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 internal bool IsNavigating { get; private set; }

public event EventHandler CanExecuteChanged;

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

public async void Execute(object parameter)
{
var parameters = parameter.ToNavigationParameters(TargetElement);

IsNavigating = true;
try
{
RaiseCanExecuteChanged();

var navigationService = Navigation.GetNavigationService(SourcePage);
await HandleNavigation(parameters, navigationService);
}
catch (Exception ex)
{
Log(ex, parameters);
}
finally
{
IsNavigating = false;
RaiseCanExecuteChanged();
}
}

protected async Task HandleNavigation(INavigationParameters parameters, INavigationService navigationService)
{
var result = await navigationService.SelectTabAsync(Name, parameters);
if(!result.Success)
{
Log(result.Exception, parameters);
}
}

protected override ICommand ProvideValue() =>
this;

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());
}

protected void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}

0 comments on commit f4ff482

Please sign in to comment.