📜  xamarin 构造函数注入 - 任何代码示例

📅  最后修改于: 2022-03-11 14:58:56.557000             🧑  作者: Mango

代码示例1
public class App : Xamarin.Forms.Application
{
    public App()
    {
        // Register DI
        TinyIoCContainer.Current.Register(new NumberClass(42));
        TinyIoCContainer.Current.Register();

        // Resolve DI
        var myClass = TinyIoCContainer.Current.Resolve();
        Console.Writeline(myClass.ToString()); //42

    }
}

public class MyClass
{
    readonly INumber _number;

    public int MyClass(INumber number)
    {
         _number = number;
    }

    public override ToString() => _number.Num;
}

public class NumberClass : INumber
{
    public NumberClass(int number)
    {
        Num = number;
    }

    public int Num { get; }
}

public interface INumber
{
    int Num { get; }
}