xabre / xamarin-forms-tab-badge

Xamarin Forms bindable Tab badges for iOS, Android, UWP, MacOS and WPF
MIT License
307 stars 75 forks source link

[iOS Bug] IndexOutOfRange when setting the Text property #62

Closed Phenek closed 5 years ago

Phenek commented 5 years ago

My implementation is like so:

            var tab = new AccountPage();
            var navigation = new NavigationPage(tab)
            {
                Title = "Profil",
                Icon = "Profile",
            };

            tab.SetBinding(TabBadge.BadgeTextProperty, nameof(BottomMenuViewModel.Count));
            tab.SetBinding(TabBadge.BadgeColorProperty, nameof(BottomMenuViewModel.Color));
            tab.SetBinding(TabBadge.BadgeTextColorProperty, nameof(BottomMenuViewModel.TextColor));
            tab.BindingContext = viewModel;
            Children.Add(navigation);

When changing the BadgeTextProperty I got an IndexOutOfRange on iOS Only. Message: Index was outside the bounds of the array.

The StackTrace take me to the Plugin.Badge.iOS.BadgedTabbedPageRenderer.OnTabbedPagePropertyChanged

Phenek commented 5 years ago

I think is about the CheckValidTabIndex function in the iOS Renderer

The function is searching for the IndexOf(tab) and need to be the IndexOf(navigation) regarding my implementation. #58

Phenek commented 5 years ago

For a workaround we could do :

        public bool CheckValidTabIndex(Page page, out int tabIndex)
        {
            tabIndex = Tabbed.Children.IndexOf(page);
            if(tabIndex == -1 && page.Parent != null)
            {
                 tabIndex = Tabbed.Children.IndexOf(page.Parent);
            }
            return tabIndex < TabBar.Items.Length && tabIndex >= 0;;
        }

Regards,

xabre commented 5 years ago

PR #63 was merged and released with v2.1.1