Can I use Xamarin.Forms.Dependency to instantiate a class that has a constructor with a parameter?

EasyGoingPatEasyGoingPat GBMember ✭✭✭

The title is basically my whole question: can I use Xamarin.Forms.Dependency to instantiate a class that has a constructor with a parameter?

All the examples I can find show interface implementations that use the default no-parameter constructor.

I'm guessing the answer to my question is 'No' because I can't see where you would pass the parameter.

Any advice or pointers to examples would be great!

Kind wishes ~ Patrick

Best Answer

  • PrashantWaliaPrashantWalia US ✭✭
    Accepted Answer

    @EasyGoingPat You can not use the constructor with Xamarin.Forms Dependency Injection. If you want to instantiate a class with Dependency Injection you can use dedicated IOC for this. I have used Splat IoC. There are multiple IoC available you can use with Xamarin.Forms like Splat, TinyIoc, AutoFac etc.

    Refer to this link for better understanding on IoC https://xamarinhelp.com/ioc-container-performance/

Answers

  • PrashantWaliaPrashantWalia USMember ✭✭
    Accepted Answer

    @EasyGoingPat You can not use the constructor with Xamarin.Forms Dependency Injection. If you want to instantiate a class with Dependency Injection you can use dedicated IOC for this. I have used Splat IoC. There are multiple IoC available you can use with Xamarin.Forms like Splat, TinyIoc, AutoFac etc.

    Refer to this link for better understanding on IoC https://xamarinhelp.com/ioc-container-performance/

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @PrashantWalia

    Thanks for the reply.

    I suspected constructors with parameters were not possible with the standard Xamarin.Forms.Dependency. When I took the dog for a long walk, I saw an easy way to re-work the code to avoid the need to do this.

    Thanks for the recommendations about IOC frameworks.

    • Patrick
Sign In or Register to comment.