-
Notifications
You must be signed in to change notification settings - Fork 3
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
Injecting services in user controls #1
Comments
I am not sure what you want to reach. As far as I know, you can not just inject UserControl as singleton, the way you do. But you have several options, one easy way is that we need to understand the life cycle of WPF and the class hierarchy, that said I might find time to come with other solutions, but for now only this one. When you start the application the first method is in MainWindow in main windows
And in your user control
I have updated the repo with this example. |
Thanks for the reply. This works very well. |
@maythamfahmi, I suggest to instead having the ServiceProvider globally accessible. Like as a static in the App class:
Then you could use this in the user control to get the services you want:
This way the user control is not dependent on the main view. |
@pekspro sorry for being late. I do not think it is a good idea to use static for dependency injection. If you find it solving your approach and ensure you do not get side effect. Honestly I have not tried that way. |
Hi,
I tried your example, and it works great until I needed to add an user control to the project.
If I inject something in the user control, the application doesn't start. I registered the usercontrol as a singleton in the app.xaml.cs together with the MainWindow.
Do you know if this is possible?
The text was updated successfully, but these errors were encountered: