Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FireController1847 opened this issue Jan 4, 2025 · 2 comments
Labels
area-architecture Issues with code structure, SDK structure, implementation details custom-handler platform/windows 🪟 t/bug Something isn't working
Milestone

Comments

@FireController1847
Copy link

FireController1847 commented Jan 4, 2025

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

@FireController1847 FireController1847 added the t/bug Something isn't working label Jan 4, 2025
@FireController1847
Copy link
Author

To further the issue, the alternate area: MauiNavigationView#TopNavArea is protected, limiting us to only being able to update specific UI elements or requiring us to provide an entirely custom implementation of MauiNavigationView. Why is this the case? Am I just doing something stupidly wrong here?

@mattleibow mattleibow added area-architecture Issues with code structure, SDK structure, implementation details custom-handler labels Jan 4, 2025
@mattleibow mattleibow added this to the Backlog milestone Jan 4, 2025
@mattleibow
Copy link
Member

@PureWeen was there an alternate/better way to do this type of customization, or did we never make the types public because we just never did?

Or, was this more a case of release was happening and we did not get the opportunity to design something that was better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-architecture Issues with code structure, SDK structure, implementation details custom-handler platform/windows 🪟 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants