Ich entwickle eine Android-Anwendung mit Xamarin. Angenommen, ich eine Aktivität haben, und es hängt davon ab, einige Schnittstellen:Xamarin Android und Dependecy Injection
interface IFoo
{
// methods
}
public class Foo : IFoo
{
private readonly IBar _bar;
public Foo(IBar bar)
{
_bar = bar;
}
// methods implementation
}
// dependency injection somewhere in Application class
var container = new UnityContainer();
container.RegisterType<IFoo, Foo>();
public class MyActivity : Activity
{
// it's incorrect constructor and this code will not be compiled
public MyActivity(IFoo foo)
{
_foo = foo;
}
private readonly IFoo _foo;
}
Ich würde Instanz IFoo in MyActivity Konstruktor injizieren wollen, aber wie Sie wissen, ist es unmöglich, Konstruktor mit Parametern zu erstellen. Wie kann ich initialisierte IFoo-Instanzen mit allen Abhängigkeiten an MyActivity übergeben? Ich benutze Unity als Dependency Injection Framework (aber ich kann es ändern).
Warum möchtest du die Instanz von "IFoo" in 'MyActivity's Konstruktor injizieren? Wenn Sie 'UnityContainer' initialisieren möchten, können Sie dies in der Klasse Android Application tun. –
@ YorkShen-MSFT und wie könnte ich es innerhalb der Aktivitätsklasse verwenden? – Pupkin