PrismLibrary / Prism

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Xamarin Forms, and Uno / Win UI Applications..
Other
6.35k stars 1.64k forks source link

[BUG] Using Rules for DryIocContainerExtensions throws NavigationException #3222

Closed RLittlesII closed 3 months ago

RLittlesII commented 3 months ago

Description

When I provide my own DryIoc container rules to the PrismAppBuilder and provide a .CreateWindow the ViewRegistry isn't able to find my page.

Steps to Reproduce

  1. Download the reproduction sample provided
  2. Run the iOS target (only target available)
  3. First navigation to the page explodes

Platform with bug

.NET MAUI

Affected platforms

iOS

Did you find any workaround?

Not currently as of opening the issue

Relevant log output

2024-08-13 16:23:20.327315-0500 Container.Rules[16298:4355938] Prism.Navigation.NavigationException: No Page has been registered with the provided key
 ---> System.Collections.Generic.KeyNotFoundException: No view with the name 'Start' has been registered
2024-08-13 16:23:20.327420-0500 Container.Rules[16298:4355938]    at Prism.Mvvm.ViewRegistryBase`1[[Microsoft.Maui.Controls.BindableObject, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].CreateView(IContainerProvider container, String name)
   at Prism.Navigation.PageNavigationService.CreatePage(String segmentName)
2024-08-13 16:23:20.327519-0500 Container.Rules[16298:4355938]    --- End of inner exception stack trace ---
   at Prism.Navigation.PageNavigationService.CreatePage(String segmentName)
   at Prism.Navigation.PageNavigationService.CreatePageFromSegment(String segment)
2024-08-13 16:23:20.327622-0500 Container.Rules[16298:4355938]    at Prism.Navigation.PageNavigationService.ProcessNavigationForRootPage(String nextSegment, Queue`1 segments, INavigationParameters parameters, Nullable`1 useModalNavigation, Nullable`1 animated)
2024-08-13 16:23:20.327745-0500 Container.Rules[16298:4355938]    at Prism.Navigation.PageNavigationService.ProcessNavigation(Page currentPage, Queue`1 segments, INavigationParameters parameters, Nullable`1 useModalNavigation, Nullable`1 animated)

Reproduction Project

Container.Rules.zip

dansiegel commented 3 months ago

This is by design. Prism.Maui will not work with the specific rule that you have:

WithDefaultIfAlreadyRegistered(IfAlreadyRegistered.Replace);

This is because of how the NavigationRegistry works in Prism.Maui where we actually use the DI Container to register an instance of ViewRegistration for each View you register for Page Navigation, Region Navigation, or for Dialogs. We then resolve all of the instances of ViewRegistration. When you use IfAlreadyRegistered.Replace this has the effect of replacing previously registered instances of ViewRegistration.