Я пытаюсь заставить работать элемент управления xaml island, он выглядит хорошо и, кажется, работает, за исключением того, что я не могу перемещаться между представлениями:
я получаю одно исключение: «NavigationView не содержит определения для Child» для
navView.SelectedItem = navView.Child.MenuItems[0];
Но я думаю, что это еще не все, чтобы сделать это.
Вот мой MainWindow.cs
private readonly List<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)>
{
("home", typeof(StartPage)),
("logs", typeof(Logs)),
};
private NavigationView navView;
private void NavView_Loaded(object sender, RoutedEventArgs e)
{
// Add handler for ContentFrame navigation.
ContentFrame.Navigated += On_Navigated;
// NavView doesn't load any page by default, so load home page.
navView.SelectedItem = navView.Child.MenuItems[0];
// If navigation occurs on SelectionChanged, this isn't needed.
// Because we use ItemInvoked to navigate, we need to call Navigate
// here to load the home page.
NavView_Navigate("home", new EntranceNavigationTransitionInfo());
}
private void NavView_ItemInvoked(NavigationView sender,
NavigationViewItemInvokedEventArgs args)
{
if (args.IsSettingsInvoked == true)
{
NavView_Navigate("settings", args.RecommendedNavigationTransitionInfo);
}
else if (args.InvokedItemContainer != null)
{
var navItemTag = args.InvokedItemContainer.Tag.ToString();
NavView_Navigate(navItemTag, args.RecommendedNavigationTransitionInfo);
}
}
private void NavView_SelectionChanged(NavigationView sender,
NavigationViewSelectionChangedEventArgs args)
{
if (args.IsSettingsSelected == true)
{
NavView_Navigate("settings", args.RecommendedNavigationTransitionInfo);
}
else if (args.SelectedItemContainer != null)
{
var navItemTag = args.SelectedItemContainer.Tag.ToString();
NavView_Navigate(navItemTag, args.RecommendedNavigationTransitionInfo);
}
}
private void NavView_Navigate(string navItemTag, NavigationTransitionInfo transitionInfo)
{
Type _page = null;
if (navItemTag == "settings")
{
_page = typeof(SettingsPage);
}
else
{
var item = _pages.FirstOrDefault(p => p.Tag.Equals(navItemTag));
_page = item.Page;
}
}
private void On_Navigated(object sender, NavigationEventArgs e)
{
if (NavView.Child is NavigationView navigationView) {
// NavView.IsBackEnabled = ContentFrame.CanGoBack;
navView = navigationView;
if (ContentFrame.Content?.GetType() == typeof(SettingsPage))
{
// SettingsItem is not part of NavView.MenuItems, and doesn't have a Tag.
navView.SelectedItem = (NavigationViewItem)navView.SettingsItem;
navView.Header = "Settings";
}
else if (ContentFrame.Content != null)
{
var item = _pages.FirstOrDefault(p => p.Page == e.Content);
navView.SelectedItem = navView.MenuItems
.OfType<NavigationViewItem>()
.First(n => n.Tag.Equals(item.Tag));
navView.Header =
((NavigationViewItem)navView.Content)?.Content?.ToString();
}
}
и вот мой xaml:
<xamlHost:WindowsXamlHost x:Name="NavView" InitialTypeName="Windows.UI.Xaml.Controls.NavigationView" Loaded="NavView_Loaded" />
<ScrollViewer>
<Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True" />
</ScrollViewer>
спасибо