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

Shell pages cannot work with DI #3147

Closed
osnipezzini opened this issue Oct 27, 2021 · 9 comments
Closed

Shell pages cannot work with DI #3147

osnipezzini opened this issue Oct 27, 2021 · 9 comments
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout fixed-in-6.0.200-preview.12 Look for this fix in 6.0.200-preview.12! t/enhancement ☀️ New feature or request
Projects

Comments

@osnipezzini
Copy link

Description

Steps to Reproduce

  1. Create a Shell menu, ContentPage and a ViewModel
  2. Configure Shell, Page and VM in DependecyInjection in builder.Services on MauiProgram
  3. Initialize App constructor with Shell, and ContentPage with ViewModel

Expected Behavior

Application starts fine

Actual Behavior

System.MissingMethodException: 'Cannot dynamically create an instance of type 'MauiProject.Pages.LoginPage'. Reason: No parameterless constructor defined.'

Basic Information

  • Version with issue: 6.0.101-preview.9.1843
  • IDE: 17.0.0-pre.7.0 - D:\ProgramData\VisualStudio\2022\Preview

Reproduction Link

https://github.com/osnipezzini/MauiShell

@osnipezzini osnipezzini added the t/bug Something isn't working label Oct 27, 2021
@Eilon Eilon added the area-controls-shell Shell Navigation, Routes, Tabs, Flyout label Oct 27, 2021
@jsuarezruiz jsuarezruiz added this to New in Triage via automation Oct 28, 2021
@PureWeen
Copy link
Member

This is more a DataTemplate enhancement then being shell specific

Making this code work with IServicePRovider would mean that Shell would now create pages via DI container

object item = LoadTemplate();

And then there's a little bit of extra work to make the global routes work with IServiceProvider

@osnipezzini
Copy link
Author

I believe you are right, but anyway this needs to be fixed as it is the central part of the project.

Or if you have an alternative I don't know.

@pictos
Copy link
Contributor

pictos commented Nov 4, 2021

Maybe you can use RouteFactoring for that. It will allow you to create your page in a more complex way. Isn't ideal but can be a workaround.

@osnipezzini
Copy link
Author

Maybe you can use RouteFactoring for that. It will allow you to create your page in a more complex way. Isn't ideal but can be a workaround.

Could you give me an example?

@PureWeen PureWeen added t/enhancement ☀️ New feature or request and removed t/bug Something isn't working labels Nov 5, 2021
@PureWeen PureWeen changed the title [Bug] Shell pages cannot work with DI Shell pages cannot work with DI Nov 5, 2021
@PureWeen PureWeen added this to the .NET 7 milestone Nov 5, 2021
@brunck
Copy link
Contributor

brunck commented Nov 5, 2021

@PureWeen I'm interested in taking a crack at this, unless you think it's something that requires work from the regular team.

@pictos
Copy link
Contributor

pictos commented Nov 5, 2021

@osnipezzini

Could you give me an example?

Yes, I did a live code session where a cover this approach, we are talking in Portuguese but you can take the code as a reference.

@PureWeen
Copy link
Member

PureWeen commented Nov 6, 2021

@brunck go for it!

I haven't dug into this too much yet so I'm not sure how tricky it'll be or won't be.

I could also be wrong with my thinking here :-)
#3147 (comment)

@brunck
Copy link
Contributor

brunck commented Nov 7, 2021

@PureWeen Thanks. Your comment makes sense, the problem I'm running into is that when the ElementTemplate constructor you mention is getting invoked, it seems to be before anything like the IApplication is getting set up with the MauiContext with the IServiceProvider that has what we need, etc. There is an IServiceProvider potentially available that we can shove in there via ProvideValue, but it's not the IServiceProvider that we need; it has things like IXamlTypeResolver in it.

@osnipezzini
Copy link
Author

@osnipezzini

Could you give me an example?

Yes, I did a live code session where a cover this approach, we are talking in Portuguese but you can take the code as a reference.

Just now i realize, what you want to say, but this is not a workaround for me, i'm using yet DI in Xamarin Forms but injecting in Page constructor with : BindingContext = Start.GetService(), but it's a good replacement for my approach

@PureWeen PureWeen closed this as completed Feb 1, 2022
Triage automation moved this from New to Done Feb 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 3, 2022
@samhouts samhouts added the fixed-in-6.0.200-preview.12 Look for this fix in 6.0.200-preview.12! label Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout fixed-in-6.0.200-preview.12 Look for this fix in 6.0.200-preview.12! t/enhancement ☀️ New feature or request
Projects
No open projects
Development

No branches or pull requests

6 participants