wpf xaml 岛无法在视图之间导航

wpf xaml island cant navigate between views

我正在尝试让 xaml 岛控件正常工作,它看起来不错而且似乎可以正常工作,只是我无法在视图之间导航:

我得到的一个例外是 "NavigationView doesnt contain a definition for Child" for

            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>

谢谢

A Windows.UI.Xaml.Controls.NavigationView 没有 Child 属性 因此要编译您的代码,您应该从以下行中删除它:

navView.SelectedItem = navView.Child.MenuItems[0];

您还需要将 navView 字段初始化为 UWP 控件的实例:

private void NavView_Loaded(object sender, RoutedEventArgs e)
{
    navView = NavView.Child as NavigationView;
    ...