Proposal: Instantiate Resources from Dependency Injection
See original GitHub issueProposal: Instantiate Resources from Dependency Injection
Summary
Use Microsoft.Extensions.DependencyInjection or other framework that implements IServiceProvider to instantiate resources to instantiate Resources from XAML…
Rationale
- Dependency Injection allows for creating loosely coupled resources such as View Models that are defined in separate or distant (not in same solution) projects.
- Current methods require requiring a “dummy instance” on a default constructor that can be instantiated by Xaml, and then removing and replacing it at runtime from the Resources.
- Will greatly improve maintainability and quality of code.
Scope
| Capability | Priority |
|---|---|
| This proposal will allow developers to use design time bindings on View Models and other objects created via Dependency Injection | Must |
This proposal will allow developers to use any dependency injection framework that implements IServiceProvider. |
Must |
This proposal will allow developers to define an Interface as a Resource since the instance will be obtained from the IServiceProvider. |
Must |
| This proposal will not require dependency injection for all projects. | Must |
Important Notes
Example
<local:IMyResource x:Key="MyResource" ServiceProvider="{x:Bind Provider}" />
Here, ServiceProvider is any instance of IServiceProvider that can be resolved via x:Bind.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Why should I use dependency injection?
Dependency inversion or injection is a technique that allows your code to decide on what implementation of a method gets called at run...
Read more >Java Dependency Injection - DI Design Pattern Example ...
We have used constructors to inject the dependencies in the application classes, another way is to use a setter method to inject dependencies...
Read more >Use dependency injection in .NET Azure Functions
Constructor injection is used to make your dependencies available in a function. The use of constructor injection requires that you do not use ......
Read more >Dependency injection in Android
Learn to build for your use case by following Google's prescriptive and opinionated guidance. ... Stay in touch with the latest releases ...
Read more >Manual dependency injection
Dependency injection is a good technique for creating scalable and testable Android apps. Use containers as a way to share instances of classes ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

https://github.com/sharpninja/WindowsAppSdkHost
Thanks for the full example! That’s much clearer.
This seems worth considering. It’s unlikely we’ll be able to get to this for the first WinUI 3.0 release, so we’ll keep it in the backlog for a future iteration.