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

MUX NavigationView should throw if WUXC NavViewItems are added to it #69

Merged
merged 9 commits into from
Jan 19, 2019
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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MenuItems [](start = 52, length = 9)

MenuItems or MenuItemsSource

}
#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
}
}