Giter VIP home page Giter VIP logo

Comments (4)

maythamfahmi avatar maythamfahmi commented on June 20, 2024 1

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.

rbasniak avatar rbasniak commented on June 20, 2024 1

Thanks for the reply. This works very well.

from wpfsampledi.

pekspro avatar pekspro commented on June 20, 2024

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

maythamfahmi avatar maythamfahmi commented on June 20, 2024

@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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.