Adopt possibility to resolve navigation Pages by type
See original GitHub issueDescription
Currently, the framework requires instantiation of a ContentPage object in order to navigate. This is quite obstructive when using constructor injection, especially when using MVU or lightweight patterns relying mostly on logic inside the code-behind.
This has been addressed in this discussion and @matt-goldman actually provided a very lightweight and straightforward solution .
I suggest considering on adopting it into the framework’s existing Navigation.
Blazor fell back to property injection which might not be ideal but at least provided a feasible solution.
Public API Changes
Check the repository’s README for reference.
Intended Use-Case
Currently:
await Navigation.PushModalAsync(new MyPage(/*requires dependencies*/));
Goldman’s solution:
await Navigation.PushModalAsync<MyPage>(); // built-in container resolves dependencies
Issue Analytics
- State:
 - Created 2 years ago
 - Reactions:9
 - Comments:9
 

Top Related StackOverflow Question
This might be occasion to reconsider how Page instantiation through the framework works. Features like default navigations currently instantiate through the parameterless constructor of a page, which worked with the legacy(?)
DependencyServicebut not with constructor injection.Markup like
<FlyoutPageItem TargetType="{x:Type page:SomePage}" />currently not only makes this impossible, but also is counter-intuitive as the Type should suffice.@marwalsch I agree with your sentiment in general, however, I’d still like to see a way to pass in constructor parameters (dependencies) to the “Create” method. It’s useful for situations where you’re binding to collections of objects/data and you’re using a
DataTemplateSelectorto convert the collection bound items into Views with companion ViewModels. For example, any implementation of ItemsView.ItemsSource.E.g. Without getting into specifics on the objects/data in a collection, consider the pattern where you’re using a
CollectionViewand binding it’sItemSourceproperty to that collection. When you specify aDataTemplateSelectorimplementation you can customize the view for each individual element of the collection based upon it’s type. When that Template is returned fromDataTemplateSelector.OnSelectTemplatethe object/data value that was provided to toOnSelectTemplateis applied to the newly created View’sBindingContextproperty. And there’s the issue; we want to have a ViewModel wrapped around that object/data value for our View to reference, not the raw value. If we have access to the DI Resolver which supports a specified constructor parameter we can use Binding (with aConverterandConverterParameterspecified) or create a customIMarkupExetensionto support the creation of the ViewModel using THE object/data value from the collection in its constructor.This pattern allows dynamic generation of ViewModels for object/data values in hierarchical data patterns in bound collections by eliminating lots of extra code work in a top level ViewModel. Which would otherwise be responsible for creating all the ViewModels up front in separate collections, and also managing any/all updates to the VM collections when data in the source collection changes.
Some rudimentary code might help illustrate:
the DataTemplateSelector:
and the TemplatedView, where the
IValueConverter.Convertmethod creates the ViewModelviewModels:DataSpecificViewModelby passing the actual value into the DI Resolver to be used in the constructor of the type.I’ve had lots of success with this pattern using DryIoc with Prism. In fact, in the Prism implementation I can supply 0 - N constructor arguments (dependencies) and DriIoc will resolve the remaining unspecified ones, if I’ve not specified them all. Thus I can have the best of both worlds, I can specify the Object/Data dependency for the ViewModel, and let the Ioc Container look up any services that VM might also require.