Hi,
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?
Posts
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.
@Adam, you can instantiate objects with non-default constructor with
<x:Arguments>
. Also described in Charles book chapter.@joacar, I found not very practical to make constructor injection for the MVVM-helpers (because they a not generally portable, no XAML-friendly etc) and personally go for Locator pattern if I need take them via DI-container. It is dumb, but easy and works. You also can add additional constructor that accept your dependencies for easer unit-testing if you want to test it.
You just blew my mind...
@ylemsoul
That is awesome.
@ylemosul That was how I ended up solving it. Will read the link to Charle's book - thanks for that!
@ylemsoul Can you provide a simple example of utilization?