Caliburn-Micro / Caliburn.Micro

A small, yet powerful framework, designed for building applications across all XAML platforms. Its strong support for MV* patterns will enable you to build your solution quickly, without the need to sacrifice code quality or testability.
http://caliburnmicro.com/
MIT License
2.8k stars 778 forks source link

Maui dependency injection #839

Closed vb2ae closed 1 year ago

vb2ae commented 1 year ago

Update the maui implementation to do dependency injection in Caliburn Micro MauiApplication instead of platform specific CaliburnApplication

Related to #831

KasperSK commented 1 year ago

What happens if I want to register platform specific services into the IoC container when it is structured like this?

vb2ae commented 1 year ago

As long as you use the same instance of the simple container in the platforms it works properly

vb2ae commented 1 year ago

Here is an example

In maui app.xaml.cs

   private static SimpleContainer _container;
    public static SimpleContainer container
    {
        get
        {
            if (_container == null)
            {
                _container = new SimpleContainer();
                _container.Instance(_container);
            }
            return _container;
        }
    }
    protected override void Configure()
    {

        container.PerRequest<IDataService, DataService>();
        container.PerRequest<MainViewModel>();
        container.PerRequest<MainView>();
    }

In windows app.xaml.cs

    protected override void Configure()
    {
       ClientNoSqlDB.Samples.Maui.App.container.PerRequest<IPlatformSpecific, WindowsInstance>();
    }

In android.app.xaml

    protected override void Configure()
    {

        ClientNoSqlDB.Samples.Maui.App.container.PerRequest<IPlatformSpecific, AndroidSpecific>();
    }

image

image