Share via

Exception has occurred: CLR/System.NullReferenceException due to DashboardTabbedPageHandler

Sreenivasan, Sreejith 885 Reputation points
2026-06-17T12:56:06.0433333+00:00

My home page is a flyout page with a tabbed page as the detail and a menu pop up page as the flyout. My problem is I have a handler for tabbed page and while using it I am getting below exception:

Exception has occurred: CLR/System.NotImplementedException

  •   An unhandled exception of type 'System.NotImplementedException' occurred in Microsoft.iOS.dll: 'The method or operation is not implemented.'    at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr* gchandle)   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)   at Inventiva.Program.Main(String[] args) in /Users/sreejith.sreenivasan/Downloads/inv-hnas337-mobile-patient/Inventiva/Platforms/iOS/Program.cs:line 13

Below is my DashboardTabbedPageHandler.cs:

using System;
using UIKit;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Controls;
using Xamarin.Forms.Clinical6.Views;

namespace MAUI.Clinical6.Platforms.iOS.Controls
{
    public class DashboardTabbedPageHandler : TabbedViewHandler
    {
        protected override void ConnectHandler(UIView platformView)
        {
            base.ConnectHandler(platformView);

            if (VirtualView is DashboardTabPage dashboardTabPage)
            {
                dashboardTabPage.DashboardBadgeEvent += OnDashboardBadgeEvent;
            }
        }

        protected override void DisconnectHandler(UIView platformView)
        {
            if (VirtualView is DashboardTabPage dashboardTabPage)
            {
                dashboardTabPage.DashboardBadgeEvent -= OnDashboardBadgeEvent;
            }

            base.DisconnectHandler(platformView);
        }

        private void OnDashboardBadgeEvent(object sender, int count)
        {
            UpdateBadge(count);
        }

        private void UpdateBadge(int count)
        {
            try
            {
                // Get the UITabBarController from the root view hierarchy
                UITabBarController tabBarController = null;

                var window = UIApplication.SharedApplication.KeyWindow;
                var rootController = window.RootViewController;

                if (rootController is UITabBarController tbc)
                {
                    tabBarController = tbc;
                }
                else if (rootController is UINavigationController nav && nav.ViewControllers.Length > 0)
                {
                    tabBarController = nav.ViewControllers[0] as UITabBarController;
                }

                if (tabBarController?.TabBar?.Items == null || tabBarController.TabBar.Items.Length == 0)
                    return;

                var tabBarItem = tabBarController.TabBar.Items[0];

                if (count == 0)
                {
                    RemoveBadge(tabBarItem);
                    return;
                }

                tabBarItem.BadgeColor = UIColor.FromRGB(190, 23, 23); // #BE1717
                tabBarItem.BadgeValue = count.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void RemoveBadge(UITabBarItem tabBarItem)
        {
            if (tabBarItem == null)
                return;

            tabBarItem.BadgeColor = UIColor.Clear;
            tabBarItem.BadgeValue = string.Empty;
        }
    }
}

I tried to remove this handler by commenting its registration in MAUIProgram.cs, but still getting another exception.

//Commecnted below
handlers.AddHandler(typeof(DashboardTabPage), typeof(DashboardTabbedPageHandler)); 

Exception getting:

Exception has occurred: CLR/System.NullReferenceException

  •   An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.iOS.dll: 'Object reference not set to an instance of an object.'    at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr* gchandle)   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)   at Inventiva.Program.Main(String[] args) in /Users/sreejith.sreenivasan/Downloads/inv-hnas337-mobile-patient/Inventiva/Platforms/iOS/Program.cs:line 13

In tabs the firsttab is a list of alerts and I want to show the unread alert count on it as a badge and that logic is inside of this handler.

Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

Nancy Vo (WICLOUD CORPORATION) 6,025 Reputation points Microsoft External Staff Moderator
2026-06-18T07:29:50.06+00:00

Hello @Sreenivasan, Sreejith ,

Thanks for your question.

The NotImplementedException is happening because your class inherits from TabbedViewHandler. In the newer .NET MAUI environment, TabbedPage on iOS is actually still rendered using a compatibility layer called TabbedRenderer. Since the system relies on this compatibility layer for tabbed pages on iOS, trying to use a purely modern view handler throws an error and stops.

The NullReferenceException comes from trying to access the KeyWindow. Apple deprecated this property a while back to support multiple screens, so MAUI often sees it as completely empty. When your code tries to pull the RootViewController from that empty window, the app crashes.

You can refer to my following code example:

using System;
using UIKit;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform;
using Xamarin.Forms.Clinical6.Views;

namespace MAUI.Clinical6.Platforms.iOS.Controls
{
    public class DashboardTabbedPageHandler : TabbedRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement is DashboardTabPage oldPage)
            {
                oldPage.DashboardBadgeEvent -= OnDashboardBadgeEvent;
            }

            if (e.NewElement is DashboardTabPage newPage)
            {
                newPage.DashboardBadgeEvent += OnDashboardBadgeEvent;
            }
        }

        private void OnDashboardBadgeEvent(object sender, int count)
        {
            UpdateBadge(count);
        }

        private void UpdateBadge(int count)
        {
            try
            {
                if (this.TabBar?.Items == null || this.TabBar.Items.Length == 0)
                    return;

                var tabBarItem = this.TabBar.Items[0];

                if (count <= 0)
                {
                    tabBarItem.BadgeColor = UIColor.Clear;
                    tabBarItem.BadgeValue = null;
                }
                else
                {
                    tabBarItem.BadgeColor = UIColor.FromRGB(190, 23, 23);
                    tabBarItem.BadgeValue = count.ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Once you update this file, make sure handlers.AddHandler(typeof(DashboardTabPage), typeof(DashboardTabbedPageHandler)); is uncommented in your MauiProgram.cs and you should be good to go

I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.