NavigationView: Throw exception when a WUX NavigationViewItem is added to it as a menu item #3232
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
With PR #69, logic was added to throw an exception with a helpful error message when a developer accidentally adds a WUX NavigationViewItem to a MUX NavigationView. Unfortunately, with the move to
ItemsRepeater
in PR #1683, this logic did not survive and thus starting with WinUI 2.4, WinUI no longer guards against adding WUX NavigationViewItems.This PR adds back this logic when a developer adds a WUX NavigationViewItem to a NavigationView (via the APIs

MenuItems
\MenuItemsSource
orFooterMenuItems
\FooterMenuItemsSource
). The exception looks like this in a WinUI C# app:Motivation and Context
Prevents developer confusion when they accidentally add a WUX NavigationViewItem to a NavigationView and see "weird" rendering behavior as a result. The image below shows the rendering difference between a MUX NavigationViewItem and a WUX NavigationViewItem when added to a MUX NavigationView:

How Has This Been Tested?
Tested manually. Unfortunately, while an API test for this scenario exists, it appears we cannot use a simple API test here because adding a WUX NavigationViewItem to the UI will lead to multiple calls of the
NavigationViewItemsFactory::GetElementCore
method during the UI update phase. Each of these calls will throw our added exception and I was unable to catch all of them and have the API test succeed. I tried adding an additionalApplication.Current.UnhandledException
handler in the API test to catch all thrown exceptions in addition to the one we catch inmicrosoft-ui-xaml/dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Line 639 in 986d48a
but the VS test explorer still showed the test as failing because they apparently still managed to slip through my added unhandled exception handling (and I set
UnhandledExceptionEventArgs.Handled
to true).Any other ideas here? If we cannot get this API test to work, we should add the reason why it is disabled as a comment (or alternatively just remove it, though keeping it will act as a documentation for this test case).