Search code examples
xamarinmvvmcrossandroid-support-library

When would one use the non-generic version of MvxAppCompatSetup<T>?


In MvvmCross there's MvxAppCompatSetup and MvxAppCompatSetup<TApplication> (which actually derives from MvxAppCompatSetup). Both are implemented in the MvxAppCompatSetup.cs file.

I really can't imagine when one would use only MvxAppCompatSetup instead of MvxAppCompatSetup<TApplication> as to my understanding one would always have the TApplication - that's actually the App that's defined in the Core project.

I am curious though: When would using the non-generic version MvxAppCompatSetup make sense / why is it there?

The code for the generic and non-generic version from the MvxAppCompatSetup.cs file:

public abstract class MvxAppCompatSetup : MvxAndroidSetup
{
    protected override IEnumerable<Assembly> AndroidViewAssemblies =>
        new List<Assembly>(base.AndroidViewAssemblies)
        {
            typeof(Toolbar).Assembly,
            typeof(DrawerLayout).Assembly,
            typeof(MvxSwipeRefreshLayout).Assembly
        };

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        return new MvxAppCompatViewPresenter(AndroidViewAssemblies);
    }

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        MvxAppCompatSetupHelper.FillTargetFactories(registry);
        base.FillTargetFactories(registry);
    }

    protected override void FillBindingNames(IMvxBindingNameRegistry registry)
    {
        MvxAppCompatSetupHelper.FillDefaultBindingNames(registry);
        base.FillBindingNames(registry);
    }
}


public class MvxAppCompatSetup<TApplication> : MvxAppCompatSetup
    where TApplication : class, IMvxApplication, new()
{
    protected override IMvxApplication CreateApp() => Mvx.IoCProvider.IoCConstruct<TApplication>();

    public override IEnumerable<Assembly> GetViewModelAssemblies()
    {
        return new[] { typeof(TApplication).GetTypeInfo().Assembly };
    }
}

Solution

  • I really can't imagine when one would use only MvxAppCompatSetup instead of MvxAppCompatSetup<TApplication> as to my understanding one would always have the TApplication - that's actually the App that's defined in the Core project.

    First of all, I would like to point out that the MvxAppCompatSetup which comes with the TApplication generic setup expects the Xamarin.Forms Application class as a parameter, which is correct. And yes you will always have the TApplication since it is a basic setup necessity in Xamarin.Forms.

    When you look at the MVVMCross Documentation for getting started. You will see the basic usage of the MvxAppCompatSetup where it is now set in the MainApplication class here.

    Now the thing is you cannot use MvxAppCompatSetup directly at all in your code since it is an Abstract class. Now the reason it is still public is what you can do is inherit from it and create your own version of setup class that can have your own setup which you yourself want to perform.

    And after going through their source code you will understand that they are using this abstract class in multiple places and hence it is public. But you as someone who is fine with the basic setup will never actually use this setup. since it cannot be initialized anyway so what you will be using is always the generic one.

    I am curious though: When would using the non-generic version MvxAppCompatSetup make sense / why is it there?

    The only reason the non-generic version being available publically is letting the developer create his own version of the setup, and hence keep it abstract so it's unusable otherwise. and the same reason the Generic version isn't abstract is that someone who is okay with the basic setup can use it

    Good luck.

    Feel free to get back in case if you have any questions or queries.