1
votes

Has the Setup class methods or hierarchy changed? I cannot seem to find a method called GetViewModelViewLookup to override in the Setup class

I am trying to map a view to a different view model. I am using MvvmCross 3.5.1

I am trying the following

protected override IDictionary<Type, Type> GetViewModelViewLookup()

but it tells me there is not a method named this to override. I am trying to follow the example on the old MvvmCross blog link

Any ideas?

Update * it looks as if the base class used to be MvxBaseSetup which contained GetViewModelToViewLookup, but now it is just MvxSetup which does not contain it.

So how do I override the viewmodel to view mapping now?

1

1 Answers

0
votes

If you just want to change the naming scheme, the function to overwrite is CreateViewToViewModelNaming

public class Setup : MvxAndroidSetup
{
    public Setup(Context applicationContext) : base(applicationContext)
    {
    }

    protected override IMvxNameMapping CreateViewToViewModelNaming()
    {
        return new ReverseViewModelNaming();
    }

    protected override IMvxApplication CreateApp()
    {
        return new Core.App();
    }

    protected override IMvxTrace CreateDebugTrace()
    {
        return new DebugTrace();
    }
}

class ReverseViewModelNaming : IMvxNameMapping
{
    public string Map(string inputName)
    {
        // MyView is presented by the view model named weiVyM (how useful :P)
        return string.Join("", inputName.Reverse());
    }
}

If you want to change the mapping, the function to overwrite is InitializeViewLookup. If you just want to add some extra mappings, call base.InitializeViewLookup().

public class Setup : MvxAndroidSetup
{
    public Setup(Context applicationContext) : base(applicationContext)
    {
    }

    protected override void InitializeViewLookup()
    {
        var registry = new Dictionary<Type, Type>()
        {
            { typeof(FirstViewModel), typeof(FirstActivity) }, 
            { typeof(HomeViewModel), typeof(HomeActivity) } ,
            { typeof(DetailViewModel), typeof(DetailActivity) }, 
            { typeof(UploadViewModel), typeof(UploadActivity) }
        };
        var container = Cirrious.CrossCore.Mvx.Resolve<IMvxViewsContainer>();
        container.AddAll(registry);
    }

    protected override IMvxApplication CreateApp()
    {
        return new Core.App();
    }

    protected override IMvxTrace CreateDebugTrace()
    {
        return new DebugTrace();
    }
}