MVVMCross Bindings in Android

后端 未结 1 1497
既然无缘
既然无缘 2020-12-11 06:58

I\'m developing a cross-platform App starting it in android. I\'ve found your MVVMCross project and I\'m trying to get into it. Right now I\'m totally new to it and don\'t k

相关标签:
1条回答
  • 2020-12-11 07:18

    To get these attributes to bind you need to include the namespace - which it looks like you've done.

    You also need to include the MvxBindingAttributes.xml file into your UI project - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross.Binding/ResourcesToCopy/MvxBindingAttributes.xml - and you must set the build action of this file to "AndroidResource"

    For an example, see any of the Android sample projects - https://github.com/slodge/MvvmCross


    For the second part of your question about adding bindings, the binding framework should automatically one-way bind (from ViewModel to View) to existing public Properties on any Monodroid View/widget.

    If the public Property isn't of the correct type (e.g. it's some Android enumeration instead of a View), then you can use an IMvxValueConverter to do the conversion.

    If you want to do 2-way binding, or there isn't a public Property for what you want to bind to, then you fairly easily do a custom binding. For an example of this, see the custom IsFavorite 2 way binding in the conference sample

    This code adds a new bindable pseudo-property "IsFavorite" to every Android Button.

    ... this is initialised in Setup.cs using code like:

        protected override void FillTargetFactories(MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
        {
            base.FillTargetFactories(registry);
    
            registry.RegisterFactory(
                        new MvxCustomBindingFactory<Button>(
                           "IsFavorite", 
                           (button) => new FavoritesButtonBinding(button)));
        }
    

    ... and the binding code is:

    public class FavoritesButtonBinding
        : MvxBaseAndroidTargetBinding
    {
        private readonly Button _button;
        private bool _currentValue;
    
        public FavoritesButtonBinding(Button button)
        {
            _button = button;
            _button.Click += ButtonOnClick;
        }
    
        private void ButtonOnClick(object sender, EventArgs eventArgs)
        {
            _currentValue = !_currentValue;
            SetButtonBackground();
            FireValueChanged(_currentValue);
        }
    
        public override void SetValue(object value)
        {
            var boolValue = (bool)value;
            _currentValue = boolValue;
            SetButtonBackground();
        }
    
        private void SetButtonBackground()
        {
            if (_currentValue)
            {
                _button.SetBackgroundResource(Resource.Drawable.star_gold_selector);
            }
            else
            {
                _button.SetBackgroundResource(Resource.Drawable.star_grey_selector);
            }
        }
    
        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                _button.Click -= ButtonOnClick;
            }
            base.Dispose(isDisposing);
        }
    
        public override Type TargetType
        {
            get { return typeof(bool); }
        }
    
        public override MvxBindingMode DefaultMode
        {
            get { return MvxBindingMode.TwoWay; }
        }
    }
    
    0 讨论(0)
提交回复
热议问题