Comments (4)
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 InitializeComponent()
called. This method will initialize UserControl class first. so what can we do with that. Here is my suggestion:
in main windows
public readonly ILogBase LogBase;
public static MainWindow AppWindow;
public MainWindow(ILogBase logBase)
{
AppWindow = this;
LogBase = logBase;
InitializeComponent();
}
And in your user control
private readonly ILogBase _logBase;
public UserControlExample()
{
InitializeComponent();
if(MainWindow.AppWindow?.LogBase != null)
_logBase = MainWindow.AppWindow.LogBase;
TestMethod();
}
I have updated the repo with this example.
from wpfsampledi.
Thanks for the reply. This works very well.
from wpfsampledi.
@maythamfahmi, I suggest to instead having the ServiceProvider globally accessible. Like as a static in the App class:
public partial class App : Application
{
public static IServiceProvider ServiceProvider;
Then you could use this in the user control to get the services you want:
public partial class UserControlExample: UserControl
{
public UserControlExample()
{
InitializeComponent();
DataContext = App.ServiceProvider.GetRequiredService<ILogBase>();
}
This way the user control is not dependent on the main view.
from wpfsampledi.
@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.
from wpfsampledi.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wpfsampledi.