Question

I'm trying to integrate TinyIoc with MvvmCross. I followed the instructions from https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#changing-the-ioc-container-that-mvvmcross-uses

and created an adapter

public class TinyIoCMvxIoCAdapter : MvxSingleton<IMvxIoCProvider>, IMvxIoCProvider
{
  ...
}

that implements all the methods and forwards it to the TinyIoC container. That was very straight forwared and I only had to implement some additional code to fire the callbacks when something gets subscribed for

void CallbackWhenRegistered<T>(Action action)
void CallbackWhenRegistered(Type type, Action action)

I changed Setup.cs

    protected override IMvxIoCProvider CreateIocProvider()
    {
        var provider = TinyIoCAdapterSetup.CreateIocProvider();
        return provider;
    }

with

 public class TinyIoCAdapterSetup
 {
    public static  IMvxIoCProvider CreateIocProvider()
    {
        var container = TinyIoCContainer.Current;
        container.AutoRegister(t => t == typeof(IMvxViewModel));
        return new TinyIoCMvxIoCAdapter(container);
    }
}

That all works great. I can see that register is called on TinyIoc and things are getting resovled as well.

What does not work are the plugins. We are using the Messenger plugin and with the TinyIoC integration, the IMvxMessenger cannot be resolved when a ViewModel is resolved that gets the IMvxMessenger ctor injected. I can see that the MessengerPluginBootstrap is created by Mvx but I couldn't see that a call was made to register IMvxMessenger.

Does anybody know what I'm doing wrong?

Was it helpful?

Solution

Each plugin has a PluginLoader class that the Bootstrapper calls to register the plugin in the IoC container.

It looks something like this:

public class PluginLoader
    : IMvxPluginLoader
{
    public static readonly PluginLoader Instance = new PluginLoader();

    private bool _loaded;

    public void EnsureLoaded()
    {
        if (_loaded)
        {
            return;
        }

        Mvx.RegisterSingleton<IMvxMessenger>(new MvxMessengerHub());
        _loaded = true;
    }
}

Without seeing your IoC adapter, it's difficult to say what the issue is. Try manually registering the plugin to see if the IoC container is working properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top