Search code examples
androidxamarin.androidmvvmcross

MvxSpinner/MvxListView doesn't work with MvvmCross Light (Chimp) from axml


In Android project I'm trying to add databindings using CrossLight part of MvvmCross. Bindings to standard TextView/Buttons work great. But simplest markup with Mvx.Control:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
  <Mvx.MvxListView />
</LinearLayout>

Gives an error

"Binary XML file line #1: Error inflating class Mvx.MvxListView"

The same thing is with Mvx.Spinner. However, when instantiating it from code in Activity.OnCreate:

_bindingContext = new MvxAndroidBindingContext(this, new LayoutInflaterProvider(LayoutInflater), _viewModel);
var view = (LinearLayout)_bindingContext.BindingInflate(Resource.Layout.Main, null);
SetContentView(view);
var spinner = new MvxSpinner(this, null, new MvxAdapter(this, _bindingContext));
view.AddView(spinner);

Everything works great (including bindings). What am I doing wrong? Is this scenario supported in general? Or maybe I should reference anything else except nuget MvvmCross.HotTuna.CrossCore?

P.S. Haven't found any samples with custom controls and CrossLight neither on github, nor on N+1 videos


Solution

  • If you want to use namespace abbreviations within your non-MvvmCross application, then you'll need to add those abbreviations. This can be done using a custom binding builder or using a 'light' setup step like:

        var viewResolver = Mvx.Resolve<IMvxAxmlNameViewTypeResolver>();
        viewResolver.ViewNamespaceAbbreviations["Mvx"] = "Cirrious.MvvmCross.Binding.Droid.Views";
        viewResolver.ViewNamespaceAbbreviations["MyApp"] = "MyApp.Controls";
    

    When doing this within a full MvvmCross application, then you can override the Setup property ViewNamespaceAbbreviations

        protected override IDictionary<string, string> ViewNamespaceAbbreviations
        {
            get
            {
                var toReturn = base.ViewNamespaceAbbreviations;
                toReturn["MyApp"] = "MyApp.UI.Droid.Controls";
                return toReturn;
            }
        }