Forum Xamarin.Forms

Instantiate Extension class in XAML with constructor injection

joacarjoacar USMember ✭✭


I have created a LocalizeExtension to use in XAML (xmlns:i18n="clr-namespace:OrderCentral.Helpers;assembly=OrderCentral") and it uses a constructror that expects an instance of ILocalize. Everything is registered with the dependency injection service (AutoFac) but it fails to properly locate the ILocalize when to instantiate LocalizeExtension. Resolving the dependency directly from the container works so it has been registered.

How is the correct way of doing this?


  • adamkempadamkemp USInsider, Developer Group Leader mod

    You can't call non-default constructors from XAML. Typically a XAML-friendly class will have a default constructor and then use public properties to configure them.

  • adamkempadamkemp USInsider, Developer Group Leader mod

    you can instantiate objects with non-default constructor with . Also described in Charles book chapter.

    You just blew my mind...

  • NMackayNMackay GBInsider, University admin


    That is awesome.

  • joacarjoacar USMember ✭✭

    @ylemosul That was how I ended up solving it. Will read the link to Charle's book - thanks for that!

  • BuhhSathBuhhSath USMember ✭✭

    @ylemsoul Can you provide a simple example of utilization?

Sign In or Register to comment.