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

Injecting services in user controls #1

Closed
rbasniak opened this issue Mar 20, 2020 · 4 comments
Closed

Injecting services in user controls #1

rbasniak opened this issue Mar 20, 2020 · 4 comments

Comments

@rbasniak
Copy link

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?

@maythamfahmi
Copy link
Owner

maythamfahmi commented Mar 20, 2020

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.

@rbasniak
Copy link
Author

Thanks for the reply. This works very well.

@pekspro
Copy link

pekspro commented Oct 4, 2021

@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.

@maythamfahmi
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants