Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement NavigateFromAsync method without recreating a page. #3182

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Maui/Prism.Maui/Navigation/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public interface INavigationService
/// </example>
Task<INavigationResult> NavigateAsync(Uri uri, INavigationParameters parameters);

/// <summary>
/// Initiates navigation from the <paramref name="viewName"/> using the specified <paramref name="route"/>.
/// </summary>
/// <param name="viewName">The name of the View to navigate from</param>
/// <param name="route">The route Uri to navigate from that view</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns>If <c>true</c> a navigate from operation was successful. If <c>false</c> the navigate from operation failed.</returns>
Task<INavigationResult> NavigateFromAsync(string viewName, Uri route, INavigationParameters parameters);

/// <summary>
/// Selects a Tab of the TabbedPage parent and Navigates to a specified Uri
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ public static Task<INavigationResult> NavigateAsync(this INavigationService navi
return navigationService.NavigateAsync(name, GetNavigationParameters(parameters));
}

/// <summary>
/// Initiates navigation to the target specified by the <paramref name="viewName"/> from the <paramref name="route"/>.
/// </summary>
/// <param name="viewName">The name of the View to navigate to</param>
/// <param name="route">The route Uri to navigate to</param>
/// <returns>If <c>true</c> a navigate from operation was successful. If <c>false</c> the navigate from operation failed.</returns>
public static Task<INavigationResult> NavigateFromAsync(this INavigationService navigationService, string viewName, Uri route) =>
navigationService.NavigateFromAsync(viewName, route, new NavigationParameters());

/// <summary>
/// Provides an easy to use way to provide an Error Callback without using await NavigationService
/// </summary>
Expand Down
65 changes: 65 additions & 0 deletions src/Maui/Prism.Maui/Navigation/PageNavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,71 @@
}
}

/// <inheritdoc />
public virtual async Task<INavigationResult> NavigateFromAsync(string viewName, Uri route, INavigationParameters parameters)
{
await WaitForPendingNavigationRequests();

try
{
if (route.IsAbsoluteUri) throw new NavigationException(NavigationException.UnsupportedAbsoluteUri);

parameters ??= new NavigationParameters();

NavigationSource = PageNavigationSource.NavigationService;

var navigationSegments = UriParsingHelper.GetUriSegments(route);

// Find a page that matches the viewName.
var currentPage = GetCurrentPage();
var navigationPages = currentPage.Navigation.NavigationStack.ToList();
navigationPages.Reverse();
var foundPage = navigationPages.FirstOrDefault(page => ViewModelLocator.GetNavigationName(page) == viewName);
if (foundPage is null)
{
// Find a page from parents.
var page = currentPage;
while (page != null)
{
if (page is not null && ViewModelLocator.GetNavigationName(page) == viewName)
break;

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 363 in src/Maui/Prism.Maui/Navigation/PageNavigationService.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

'Page' does not contain a definition for 'GetParentPage' and no accessible extension method 'GetParentPage' accepting a first argument of type 'Page' could be found (are you missing a using directive or an assembly reference?)
page = page.GetParentPage();
}
currentPage = page;
}
else
{
// Insert RemovePageSegment.
var removePageCount = navigationPages.IndexOf(foundPage);

var tempQueue = new Queue<string>();
for (int i = 0; i < removePageCount; i++)
{
tempQueue.Enqueue(RemovePageSegment);
}
while(navigationSegments.Count > 0)
{
tempQueue.Enqueue(navigationSegments.Dequeue());
}
navigationSegments = tempQueue;
}

await ProcessNavigation(currentPage, navigationSegments, parameters, null, null);

return Notify(route, parameters);
}
catch (Exception ex)
{
return Notify(route, parameters, ex);
}
finally
{
_lastNavigate = DateTime.Now;
NavigationSource = PageNavigationSource.Device;
_semaphore.Release();
}
}

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Prism.Core/Navigation/NavigationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class NavigationException : Exception
/// </summary>
public const string UnknownException = "An unknown error occurred. You may need to specify whether to Use Modal Navigation or not.";

/// <summary>
/// The <see cref="NavigationException"/> Message returned when an absolute path is specified but not supported.
/// </summary>
public const string UnsupportedAbsoluteUri = "An unsupported absolute uri. Please use a relative URI.";

/// <summary>
/// Initializes a new instance of the <see cref="NavigationException"/>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,58 @@ public async Task Navigation_Animation_IsFalse()
Assert.False(push.Animated);
}

[Fact]
public async Task Navigation_FromPageUsingRoute()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("NavigationPage/MockViewA/MockViewB/MockViewC"))
.Build();
var window = GetWindow(mauiApp);
var navigationService = Prism.Navigation.Xaml.Navigation.GetNavigationService(window.CurrentPage);
var currentNavigationStackUri = string.Join("/", window.CurrentPage.Navigation.NavigationStack.Select(v => v.GetType().Name));
Assert.Equal("MockViewA/MockViewB/MockViewC", currentNavigationStackUri);

var result = await navigationService.NavigateFromAsync("MockViewA", UriParsingHelper.Parse("MockViewD/MockViewE"), null);

Assert.True(result.Success);
currentNavigationStackUri = string.Join("/", window.CurrentPage.Navigation.NavigationStack.Select(v => v.GetType().Name));
Assert.Equal("MockViewA/MockViewD/MockViewE", currentNavigationStackUri);
}

[Fact]
public async Task Navigation_FromRootNavigationPageUsingRoute()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("NavigationPage/MockViewA/MockViewB/MockViewC"))
.Build();
var window = GetWindow(mauiApp);
var navigationService = Prism.Navigation.Xaml.Navigation.GetNavigationService(window.CurrentPage);
var currentNavigationStackUri = string.Join("/", window.CurrentPage.Navigation.NavigationStack.Select(v => v.GetType().Name));
Assert.Equal("MockViewA/MockViewB/MockViewC", currentNavigationStackUri);

var result = await navigationService.NavigateFromAsync("NavigationPage", UriParsingHelper.Parse("MockViewD/MockViewE"), null);

Assert.True(result.Success);
currentNavigationStackUri = string.Join("/", window.CurrentPage.Navigation.NavigationStack.Select(v => v.GetType().Name));
Assert.Equal("MockViewD/MockViewE", currentNavigationStackUri);
}

[Fact]
public async Task Navigation_FromFlyoutPageUsingRoute()
{
var mauiApp = CreateBuilder(prism => prism.CreateWindow("MockHome/NavigationPage/MockViewA"))
.Build();
var window = GetWindow(mauiApp);

var mockHome = (FlyoutPage)window.Page;
var navigationPage = (NavigationPage)mockHome.Detail;
Assert.IsType<MockViewA>(navigationPage.CurrentPage);

var navigationService = Prism.Navigation.Xaml.Navigation.GetNavigationService(window.CurrentPage);
var result = await navigationService.NavigateFromAsync("MockHome", UriParsingHelper.Parse("MockViewB"), null);

Assert.True(result.Success);
Assert.IsType<MockViewB>(mockHome.Detail);
}

[Theory]
[InlineData("MockViewA", "MockViewB", null)]
[InlineData("NavigationPage/MockViewA", "MockViewB?useModalNavigation=true", true)]
Expand Down
Loading