Skip to content

Commit 2e2a14d

Browse files
committed
Fix navigating to unregistered region error
The null check was the wrong away around in the content loader, I've flipped it and added a test to catch the erroneous behavior
1 parent b11acae commit 2e2a14d

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Maui/Prism.Maui/Navigation/Regions/Navigation/RegionNavigationContentLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected virtual IEnumerable<VisualElement> GetCandidatesFromRegion(IRegion reg
115115
{
116116
var registry = region.Container().Resolve<IRegionNavigationRegistry>();
117117
var registration = registry.Registrations.FirstOrDefault(x => x.Type == ViewType.Region && (x.Name == candidateNavigationContract || x.View.Name == candidateNavigationContract || x.View.FullName == candidateNavigationContract));
118-
if (registration is null)
118+
if (registration is not null)
119119
{
120120
RegionNavigationContentLoader.GetCandidatesFromRegionViews(region, registration.View.FullName);
121121
}

tests/Maui/Prism.DryIoc.Maui.Tests/Fixtures/Regions/RegionFixture.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Prism.DryIoc.Maui.Tests.Mocks.ViewModels;
1+
using Prism.DryIoc.Maui.Tests.Mocks.ViewModels;
22
using Prism.DryIoc.Maui.Tests.Mocks.Views;
33
using Prism.Navigation.Xaml;
44

@@ -251,4 +251,39 @@ public async Task Region_IsDestroyed_OnNavigationGoBack()
251251
var result = await navigationService.NavigateAsync("RegionPage");
252252
Assert.True(result.Success);
253253
}
254+
255+
[Fact]
256+
public void Issue3328_WhenNavigatingToUnregisteredView_ShouldFailWithKeyNotFoundException()
257+
{
258+
// Arrange
259+
var mauiApp = CreateBuilder(prism => prism.RegisterTypes(container =>
260+
{
261+
container.RegisterForNavigation<MockContentRegionPage, MockContentRegionPageViewModel>();
262+
container.RegisterForRegionNavigation<MockRegionViewA, MockRegionViewAViewModel>();
263+
}).CreateWindow(nav => nav.NavigateAsync("MockContentRegionPage"))).Build();
264+
var window = GetWindow(mauiApp);
265+
266+
Assert.IsType<MockContentRegionPage>(window.Page);
267+
var page = window.Page as MockContentRegionPage;
268+
Assert.NotNull(page.ContentRegion.Content);
269+
Assert.IsType<MockRegionViewA>(page.ContentRegion.Content);
270+
Assert.IsType<MockRegionViewAViewModel>(page.ContentRegion.Content.BindingContext);
271+
272+
// Act
273+
var regionManager = mauiApp.Services.GetRequiredService<IRegionManager>();
274+
INavigationResult result = null;
275+
276+
regionManager.RequestNavigate("ContentRegion", "UnregisteredRegion", navResult =>
277+
{
278+
result = navResult;
279+
});
280+
281+
// Assert
282+
Assert.False(result.Success);
283+
var ex = Assert.IsType<KeyNotFoundException>(result.Exception);
284+
Assert.Equal("No view with the name 'UnregisteredRegion' has been registered", ex.Message);
285+
286+
Assert.IsType<MockRegionViewA>(page.ContentRegion.Content);
287+
Assert.IsType<MockRegionViewAViewModel>(page.ContentRegion.Content.BindingContext);
288+
}
254289
}

0 commit comments

Comments
 (0)