-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3068 from PrismLibrary/dev/ds/getnavservice
Add NavigationService helper from the IWindowManager
- Loading branch information
Showing
7 changed files
with
122 additions
and
71 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 was deleted.
Oops, something went wrong.
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
22 changes: 22 additions & 0 deletions
22
src/Maui/Prism.Maui/Navigation/IWindowManagerExtensions.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,22 @@ | ||
namespace Prism.Navigation; | ||
|
||
/// <summary> | ||
/// Provides extensions for the <see cref="IWindowManager"/> | ||
/// </summary> | ||
public static class IWindowManagerExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the <see cref="INavigationService"/> for the currently displayed <see cref="Page"/>. | ||
/// </summary> | ||
/// <param name="windowManager">The <see cref="IWindowManager"/>.</param> | ||
/// <returns>The <see cref="INavigationService"/> for the current <see cref="Page"/>.</returns> | ||
public static INavigationService GetCurrentNavigationService(this IWindowManager windowManager) | ||
{ | ||
var window = windowManager.Windows.OfType<PrismWindow>().First(); | ||
|
||
if (window.CurrentPage is null) | ||
throw new InvalidOperationException("No current page has been set."); | ||
|
||
return Xaml.Navigation.GetNavigationService(window.CurrentPage); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Navigation/WindowManagerTests.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,37 @@ | ||
| ||
using Prism.Navigation.Xaml; | ||
|
||
namespace Prism.DryIoc.Maui.Tests.Fixtures.Navigation; | ||
|
||
public class WindowManagerTests : TestBase | ||
{ | ||
public WindowManagerTests(ITestOutputHelper testOutputHelper) | ||
: base(testOutputHelper) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData("NavigationPage/MockViewA/MockViewB/MockViewC")] | ||
[InlineData("MockHome/NavigationPage/MockViewA")] | ||
public void WindowManagerGetsNavigationServiceFromCurrentPage(string uri) | ||
{ | ||
var mauiApp = CreateBuilder(prism => prism.CreateWindow(uri)) | ||
.Build(); | ||
var window = GetWindow(mauiApp); | ||
|
||
var rootPage = window.Page; | ||
var currentPage = rootPage; | ||
if (rootPage is NavigationPage navigationPage) | ||
{ | ||
currentPage = navigationPage.CurrentPage; | ||
} | ||
if (rootPage is FlyoutPage flyoutPage && flyoutPage.Detail is NavigationPage detailPage) | ||
{ | ||
currentPage = detailPage.CurrentPage; | ||
} | ||
|
||
var currentNavigationService = Prism.Navigation.Xaml.Navigation.GetNavigationService(currentPage); | ||
var windowManager = rootPage.GetContainerProvider().Resolve<IWindowManager>(); | ||
Assert.Same(currentNavigationService, windowManager.GetCurrentNavigationService()); | ||
} | ||
} |