0

I am trying to bind to the badgevalue of a TabBarItem like this:

var set = this.CreateBindingSet<MyView, MyViewModel>();
set.Bind(ViewControllers[0].TabBarItem.BadgeValue).To(vm => vm.MyNumber);
set.Apply();

but I get the following error:

MvxBind: Error:   6.30 Empty binding target passed to MvxTargetBindingFactoryRegistry

However if I set the value directly like this the badge appears:

ViewControllers[0].TabBarItem.BadgeValue = ((MyViewModel)ViewModel).MyNumber;

Why is the binding not working? Thanks!

doorman
  • 15,707
  • 22
  • 80
  • 145

2 Answers2

2

This doesn't work because you need to set up your own binding extensions if what you are trying to bind to doesn't exist as a valid binding target already.

Look here where Stuart answered a similar question for Android.

Community
  • 1
  • 1
PkL728
  • 955
  • 8
  • 21
  • Thanks for the hint. For ios I found this presentation about custom bindings on ios [link](https://www.youtube.com/watch?feature=player_detailpage&v=taBoOenbpiU&t=1673s) – doorman Dec 05 '14 at 18:40
1

Create a custom binding:

public class TabBadgeBinding : MvxTargetBinding
{
    public const string TargetPropertyName = nameof(TabBadgeBinding);

    private readonly UIViewController _viewController;

    public TabBadgeBinding(UIViewController viewController) : base(viewController)
    {
        _viewController = viewController;
    }

    public override Type TargetType => typeof(UIViewController);

    public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;

    public override void SetValue(object value)
    {
        if (value is string badgeValue)
        {
            _viewController.TabBarItem.BadgeValue = badgeValue;
        }
    }
}

Register Binding in Setup.cs:

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);
        registry.RegisterFactory(new MvxCustomBindingFactory<UIViewController>(TabBadgeBinding.TargetPropertyName, (controller) => new TabBadgeBinding(controller)));
    }

Use Binding in your VC:

    set.Bind(ParentViewController).For(TabBadgeBinding.TargetPropertyName).To(vm => vm.UnreadNotificationsCount);
c.lamont.dev
  • 693
  • 1
  • 6
  • 14