FreshMVVM and XLabs

Hi together,

I’m currently implementing an app with Xamarin.Forms for all three platforms. Looks good so far using FreshMvvm-Framework.
For having some extras in the app (geolocation, calendar, a password-EntryCell and some more), I'd like to include XLabs.Forms, but I’m not sure if it’s IoC-Logic conflicts with FreshMVVM’s (honestly, I don’t like the idea of having multiple IoC-Providers in one application).
Can anybody say if it’s possible without problems to combine them?

Thanks, Sascha

Tagged:

Answers

  • MichaelRidlandMichaelRidland AUInsider, University ✭✭✭

    Hi Sascha

    You shouldn't need to use the XLabs IOC containers, you should be able to use only the FreshMvvm IOC container. The advantage of using the FreshMvvm IOC container is that it will work for your PageModel constructor injection.

    Thanks

  • Hi Michael,
    I did ask because the XLabs.Forms NuGet-Package has a dependency to XLabs.IoC.
    This package does not conflict with FreshMvvm-IoC?

    Thanks, Sascha

  • SKallSKall USMember ✭✭✭✭

    The dependency to XLabs.IoC is needed if you intend to use HybridWebView using XAML as the HybridWebView needs a JSON serializer implementation.

    https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms/Controls/HybridWebView.cs#L92-L105

    If you need to you probably could wrap the FreshMvvm-IoC to provide XLabs.IoC container functionality. I am not familiar with how that package works but XLabs.IoC was designed to be plugin based so you can use many existing DI containers with it. The minimum needed functionality would be to implement IResolver around the FreshMvvm-IoC container.

    https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/IOC/XLabs.Ioc/IResolver.cs

  • JohnDohertyJohnDoherty USUniversity ✭✭

    If anyone else comes across this, the following approach works for me:

    public class FreshTinyIoCResolver : IResolver
    {
    
        public bool IsRegistered(Type type)
        {
            return FreshTinyIoC.FreshTinyIoCContainer.Current.CanResolve(type);
        }
    
        public bool IsRegistered<T>() where T : class
        {
            return FreshTinyIoC.FreshTinyIoCContainer.Current.CanResolve<T>();
        }
    
        public object Resolve(Type type)
        {
            return FreshTinyIoC.FreshTinyIoCContainer.Current.Resolve(type);
        }
    
        public T Resolve<T>() where T : class
        {
            return FreshTinyIoC.FreshTinyIoCContainer.Current.Resolve<T>();
        }
    
        public IEnumerable<object> ResolveAll(Type type)
        {
            return FreshTinyIoC.FreshTinyIoCContainer.Current.ResolveAll(type);
        }
    
        public IEnumerable<T> ResolveAll<T>() where T : class
        {
            return FreshTinyIoC.FreshTinyIoCContainer.Current.ResolveAll<T>();
        }
    }
    

    Then to initialize things in XF App:

    Resolver.SetResolver(new FreshTinyIoCResolver());
    
Sign In or Register to comment.