0

I am trying to use PageRenderer to customize/reposition elements of ToolbarItem for iOS but here NavigationController throwing null reference exception. Below my code

   public class MyNavigationRenderer: PageRenderer
    {    
        public new MyNavigationBar Element
        {
            get { return (MyNavigationBar)base.Element; }
        }
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            var LeftNavList = new List<UIBarButtonItem>();
            var rightNavList = new List<UIBarButtonItem>();
            var navigationItem = this.NavigationController.TopViewController.NavigationItem;

            for (var i = 0; i < Element.ToolbarItems.Count; i++)
            {
                var reorder = (Element.ToolbarItems.Count - 1);
                var ItemPriority = Element.ToolbarItems[reorder - i].Priority;

                if (ItemPriority == 1)
                {
                   UIBarButtonItem LeftNavItems = navigationItem.RightBarButtonItems[i];
                   LeftNavList.Add(LeftNavItems);
                }
                else if (ItemPriority == 0)
                {
                   UIBarButtonItem RightNavItems = navigationItem.RightBarButtonItems[i];
                   rightNavList.Add(RightNavItems);
                }
            }
             navigationItem.SetLeftBarButtonItems(LeftNavList.ToArray(), false);
             navigationItem.SetRightBarButtonItems(rightNavList.ToArray(), false);
        }
    }

Below MyNavigationBar.cs class in portable/shared forms project

public class MyNavigationBar : NavigationPage
{
    public MyNavigationBar(Page content) : base(content)
    {
        Init();
    }
    private void Init()
    {
        this.ToolbarItems.Add(new ToolbarItem() { Icon = "kid", Priority = 0, Order = ToolbarItemOrder.Primary });
        this.ToolbarItems.Add(new ToolbarItem() { Text = "License", Priority = 0, Order = ToolbarItemOrder.Primary });
    }    
} 

App starting

public App ()
{
    InitializeComponent();
    MainPage = new MyNavigationBar(new LoginPage());
} 

See below screenshot getting exception

enter image description here

R15
  • 13,982
  • 14
  • 97
  • 173
  • 1
    If I am not mistaken the renderer can make a few passes though there. Just check if its null and short circuit – Gerald Versluis May 03 '18 at 11:00
  • It seems to be issue only. If i try like below, I am getting null exception at `RightBarButtonItems[i]` below modified code. `var nav=new UINavigationController(); var navigationItem = nav.NavigationItem; navigationItem.RightBarButtonItems[i];` – R15 May 03 '18 at 11:46
  • I got that code from here [This](https://timeyoutake.it/2016/01/02/creating-a-left-toolbaritem-in-xamarin-forms/) – R15 May 03 '18 at 11:48
  • I think your `NavigationController` does not have a `TopViewController`. Or can you - standing in the debugger as your screenshots shows - check what's null there? – Waescher May 03 '18 at 22:12
  • Here `NavigationController` is null, I checked it. – R15 May 04 '18 at 06:29

1 Answers1

0

I faced this issue, but in my case, I was trying to get NavigationController from content page which didn't had NavigationController, make sure you null check before calling TopViewController,

 var navController = this.NavigationController;
 if(navController == null)
 {
     return;
 }
 UINavigationItem navigationItem = navController.TopViewController.NavigationItem;

For example, When User opens the app, he will be presented with Login page, which didn't had any Navigation Bar.

Arjun K R
  • 73
  • 6