Catch toolbarItems changes

TheoBTheoB FRMember ✭✭

Hello,
I created the following renderer on iOS to avoid having the secondary row of toolbarItems

[assembly: ExportRenderer(typeof(ContentPage), typeof(MyNamespace.CustomNavigationPageRenderer))]
namespace MyNamespace.iOS.CustomRenderer
{
    public class CustomNavigationPageRenderer : PageRenderer
    {

        List<ToolbarItem> _secondaryItems;

        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            if (e.NewElement is ContentPage page) {
                _secondaryItems = page.ToolbarItems.Where(i => i.Order == ToolbarItemOrder.Secondary).ToList();
                _secondaryItems.ForEach(t => page.ToolbarItems.Remove(t));
            }
            base.OnElementChanged(e);
        }

        public override void ViewWillAppear(bool animated)
        {
            var element = (ContentPage)Element;

            //If global secondary toolbar items are not null, I created and added a primary toolbar item with image(Overflow) I         
            // want to show.
            if (_secondaryItems != null && _secondaryItems.Count > 0) {
                var item = new ToolbarItem() {
                    Order = ToolbarItemOrder.Primary,
                    Icon = "more",
                    Priority = 1,
                    Command = new Command(ToolClicked)
                };
                element.ToolbarItems.Add(item);
            }

            base.ViewWillAppear(animated);
        }

        //Create a table instance and added it to the view.
        private void ToolClicked()
        {
            var alert = UIAlertController.Create(null, null, UIAlertControllerStyle.ActionSheet);
            foreach (var secondaryItem in _secondaryItems) {
                var uiAlertAction = UIAlertAction.Create(secondaryItem.Text, UIAlertActionStyle.Default, action => secondaryItem.Command.Execute(secondaryItem.CommandParameter));
                if (!string.IsNullOrEmpty(secondaryItem?.Icon?.File)) uiAlertAction.SetValueForKey(new UIImage(secondaryItem.Icon.File), new NSString("image"));
                alert.AddAction(uiAlertAction);
            }
            alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
            PresentViewController(alert, true, null);
        }

    }
}

https://us.v-cdn.net/5019960/uploads/editor/t4/a103h4xxiqf5.png

It works just fine, except when I change my ToolBarItems through a Binding it reverts to the "original" appearance.
Can someone tell me what to add in order to catch the change and update my secondary items ?

Thanks in advance !

Answers

  • TheoBTheoB FRMember ✭✭

    I have found an extremely dirt awful way to do it by adding this code in ViewWillAppear

        public override void ViewWillAppear(bool animated)
        {
            var element = (ContentPage)Element;
    
            Element.PropertyChanged += (sender, e) => {
                if (e.PropertyName == "ToolBarItems" && sender is InstanceDetailPage) {
                    _secondaryItems = ((InstanceDetailPage)sender).ToolBarItems.Where(i => i.Order == ToolbarItemOrder.Secondary).ToList();
                    _secondaryItems.ForEach(t => ((InstanceDetailPage)sender).ToolBarItems.Remove(t));
    
                    if (_secondaryItems != null && _secondaryItems.Count > 0) {
                        var item = new ToolbarItem() {
                            Order = ToolbarItemOrder.Primary,
                            Icon = "more",
                            Priority = 1,
                            Command = new Command(ToolClicked)
                        };
                        ((InstanceDetailPage)sender).ToolBarItems.Add(item);
                    }
                }
            };
    [...]
    

    This only works in one very specific situation. Does anyone know of a more generic approach ?

Sign In or Register to comment.