Skip to content

Instantly share code, notes, and snippets.

@dansiegel
Created May 30, 2018 19:59
Show Gist options
  • Save dansiegel/963c90ff36e5b70a679833fb28289536 to your computer and use it in GitHub Desktop.
Save dansiegel/963c90ff36e5b70a679833fb28289536 to your computer and use it in GitHub Desktop.
Prism Tabbed Navigation
using System;
using System.Linq;
using System.Threading.Tasks;
using Prism.Common;
using Xamarin.Forms;
namespace Prism.Navigation
{
public static class INavigationServiceExtensions
{
public static async Task<INavigationResult> NavigateToTab(this INavigationService navigationService, string tabName, INavigationParameters parameters)
{
if (parameters == null)
parameters = new NavigationParameters();
var result = new NavigationResult { Success = true };
try
{
var page = (navigationService as IPageAware).Page;
TabbedPage tabbedPage = null;
switch(page.Parent)
{
case TabbedPage tabbed:
tabbedPage = tabbed;
break;
case NavigationPage navigationPage;
if (navigationPage.Parent is TabbedPage parent)
tabbedPage = parent;
break;
}
if (tabbedPage == null)
throw new NullReferenceException("The current page does not appear to have a TabbedPage parent");
if(PageUtilities.CanNavigate(page, parameters) && await PageUtilities.CanNavigateAsync(page, parameters))
{
tabbedPage.CurrentPage = tabbedPage.Children.First(c => c.GetType() == PageNavigationRegistry.GetPageType(tabName));
PageUtilities.OnNavigatedFrom(page, parameters);
PageUtilities.OnNavigatedTo(tabbedPage.CurrentPage, parameters);
}
}
catch (Exception ex)
{
result.Success = false;
result.Exception = ex;
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment