Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
85 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains 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
73 changes: 73 additions & 0 deletions
73
Source/Xamarin/Prism.Forms/Navigation/Xaml/SelectTabExtension.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |