Skip to content

Commit

Permalink
MUX NavigationView should throw if WUXC NavViewItems are added to it (#…
Browse files Browse the repository at this point in the history
…69)

MUX NavigationView should throw if WUXC NavViewItems are added to it

Use IsItemItsOwnContainerOverride as a hook to approximate list contents change events and deny any WUX items in the MUX list.

[Internal Issue](https://microsoft.visualstudio.com/OS/ft_xamlcon/_workitems/edit/19017881)
  • Loading branch information
chrisglein authored Jan 19, 2019
1 parent 8e7f7d6 commit 0e052f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
12 changes: 11 additions & 1 deletion dev/NavigationView/NavigationViewList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ bool NavigationViewList::IsItemItsOwnContainerOverride(winrt::IInspectable const
bool isItemItsOwnContainerOverride = false;
if (args)
{
// This validation is only relevant outside of the Windows build where WUXC and MUXC have distinct types.
#if !BUILD_WINDOWS
// Certain items are disallowed in a NavigationView's items list. Check for them.
if (args.try_as<winrt::Windows::UI::Xaml::Controls::NavigationViewItemBase>())
{
throw winrt::hresult_invalid_argument(L"MenuItems contains a Windows.UI.Xaml.Controls.NavigationViewItem. This control requires that the NavigationViewItems be of type Microsoft.UI.Xaml.Controls.NavigationViewItem.");
}
#endif

auto nvib = args.try_as<winrt::NavigationViewItemBase>();
if (nvib && nvib != m_lastItemCalledInIsItemItsOwnContainerOverride.get())
{
Expand Down Expand Up @@ -56,6 +65,7 @@ void NavigationViewList::PrepareContainerForItemOverride(winrt::DependencyObject
itemContainer.UseSystemFocusVisuals(m_showFocusVisual);
winrt::get_self<NavigationViewItem>(itemContainer)->ClearIsContentChangeHandlingDelayedForTopNavFlag();
}

__super::PrepareContainerForItemOverride(element, item);
}

Expand Down Expand Up @@ -133,4 +143,4 @@ void NavigationViewList::PropagateChangeToAllContainers(std::function<void(typen
}
}
}
}
}
33 changes: 33 additions & 0 deletions dev/NavigationView/NavigationView_ApiTests/NavigationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using MUXControlsTestApp.Utilities;

using System;
using System.Collections.Generic;
using Windows.Foundation.Metadata;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Markup;
Expand Down Expand Up @@ -362,5 +365,35 @@ public void CanLoadSimpleNavigationView()
}
#endif

#if !BUILD_WINDOWS
[TestMethod]
public void VerifyCanNotAddWUXItems()
{
if (!ApiInformation.IsTypePresent("Windows.UI.Xaml.Controls.NavigationViewItem"))
{
Log.Warning("WUX version of NavigationViewItem only available starting in RS3.");
return;
}

RunOnUIThread.Execute(() =>
{
var navView = new NavigationView();

var muxItem = new Microsoft.UI.Xaml.Controls.NavigationViewItem { Content = "MUX Item" };
navView.MenuItems.Add(muxItem);

navView.MenuItems.Add(new Microsoft.UI.Xaml.Controls.NavigationViewItemSeparator());

// No errors should occur here when we only use MUX items
navView.UpdateLayout();

var wuxItem = new Windows.UI.Xaml.Controls.NavigationViewItem { Content = "WUX Item" };
navView.MenuItems.Add(wuxItem);

// But adding a WUX item should generate an exception (as soon as the new item gets processed)
Verify.Throws<Exception>(() => { navView.UpdateLayout(); });
});
}
#endif
}
}

0 comments on commit 0e052f1

Please sign in to comment.