Skip to content

Unable to Customize Windows NavigationViewItem due to NavigationViewItemViewModel Protection Level #26953

@FireController1847

Description

@FireController1847

Description

I'm working on configuring my own handler for the Windows MauiNavigationView, so that way I can customize how one specific tab of my navigation view appears. The problem is: I can't fetch the internal rendering for the ViewItem because of the accessibility level for NavigationViewItemViewModel. I can workaround this issue at the moment using reflection, but I'd ideally be ready for NativeAOT early by coding without reflection in mind.

Notably, this class is referenced quite often throughout the platform-specific code for Windows, which tells me that it should not be an internal class, if this code shows that it is dependent for various other publicly accessible operations. Or there should be an INavigationViewItemViewModel class provided. Regardless of the solution, I do believe this is a bug.

I'd be willing to make a simple PR changing the accessibility here, but I'd like to run it by the community since I'm a newer developer to .NET MAUI, and I could be doing something wrong here; so I want to ensure this is the best solution to what I'm doing specifically.

https://github.com/dotnet/maui/blob/main/src/Core/src/Platform/Windows/NavigationViewItemViewModel.cs#L78

Steps to Reproduce

Even better: Some reproduction code.

using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Platform;
using Serilog;

namespace EmbersMusic.MAUI.Handlers {

    /// <summary>
    /// Custom handlers for ShellItems for EmbersMusic.
    /// </summary>
    public static class EmbersShellItemHandler {

#if WINDOWS
        private static bool NAVVIEW_LOADED_ONCE = false;
#endif

        /// <summary>
        /// Configures the custom handlers.
        /// </summary>
        public static void Configure() {
#if WINDOWS
            // Windows handlers
            ShellItemHandler.Mapper.AppendToMapping("ConfigureMainNavigation", WINDOWS_ConfigureMainNavigation);
#endif
        }

#if WINDOWS
        /// <summary>
        /// Configures the main navigation to appear correctly
        /// on the various platforms.
        /// </summary>
        private static void WINDOWS_ConfigureMainNavigation(ShellItemHandler handler, ShellItem view) {
            ILogger logger = Log.Logger.ForContext(typeof(EmbersShellItemHandler));
            if (handler.PlatformView is MauiNavigationView navView) {
                if (NAVVIEW_LOADED_ONCE) return;
                logger.Debug($"Navigation is {nameof(MauiNavigationView)}! Attaching load event...");
                navView.Loaded += (sender, e) => {
                    logger.Debug($"Main navigation is loaded on Windows.");
                    // How to check for items type?
                    // Can't do navView.MenuItemsSource is ObserableCollection<NavigationViewItemViewModel>
                    // Can't manually cast IList object's to NavigationViewItemViewModel
                    // Can't modify individual items at all, or even access the NavigationViewItemViewModel#Content value.
                };
                NAVVIEW_LOADED_ONCE = true;
            }
        }
#endif

    }

}

Link to public reproduction project repository

No response

Version with bug

9.0.21 SR2.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions