首页 > 解决方案 > object does not contain a definition for Navigated

问题描述

im trying to get a xaml island navigation working in my core wpf app and get the error for:

            ContentFrame.Content.Navigated += On_Navigated;

thats my mainwindow.cs:

public partial class MainWindow
{
    private NavigationView NavView;

    public MainWindow()
    {
        InitializeComponent();
    }

    // List of ValueTuple holding the Navigation Tag and the relative Navigation Page
    private readonly List<(string Tag, Type Page)> _pages = new List<(string Tag, Type Page)>
    {
        ("home", typeof(startPage)),
        ("other", typeof(otherPage)),
    };

    private void NavView_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        NavView = MyNavView.Child as NavigationView;

        // Add handler for ContentFrame navigation.
        ContentFrame.Navigated += On_Navigated;

        // NavView doesn't load any page by default, so load home page.
        NavView.SelectedItem = NavView.MenuItems[0];

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

        var preNavPageType = ContentFrame.Content?.GetType();

        // Only navigate if the selected page isn't currently loaded.
        if (!(_page is null) && !Type.Equals(preNavPageType, _page))
        {
            //ContentFrame.Navigate(_page, null, transitionInfo);
        }
    }



    private void On_Navigated(object sender, NavigationEventArgs e)
    {

        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.SourcePageType);

            NavView.SelectedItem = NavView.MenuItems
                .OfType<NavigationViewItem>()
                .First(n => n.Tag.Equals(item.Tag));

            NavView.Header =
                ((NavigationViewItem)NavView.SelectedItem)?.Content?.ToString();
        }
    }



}

thats the xaml:

<Window x:Class="GUI_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:GUI_Test"
    mc:Ignorable="d"
    xmlns:xamlHost="clr-namespace:Microsoft.Toolkit.Wpf.UI.XamlHost;assembly=Microsoft.Toolkit.Wpf.UI.XamlHost"  
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <xamlHost:WindowsXamlHost x:Name="MyNavView" InitialTypeName="Windows.UI.Xaml.Controls.NavigationView" />
    <ScrollViewer>
        <Frame x:Name="ContentFrame" Padding="12,0,12,24" IsTabStop="True"/>
    </ScrollViewer>
</Grid>

Most of the code come from: https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/navigationview, all I did was throwing things out like keyboard navigation and using in the MainWindow.xaml

        <xamlHost:WindowsXamlHost x:Name="MyNavView" InitialTypeName="Windows.UI.Xaml.Controls.NavigationView" />

Hope someone has stuff like this already done and could help me to get the nav running in core WPF.

thank you

Updated the code and getting now no errors anymore.

           private void On_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        NavView.IsBackEnabled = ContentFrame.CanGoBack;

        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?.GetType());

            NavView.SelectedItem = NavView.MenuItems
                .OfType<NavigationViewItem>()
                .First(n => n.Tag.Equals(item.Tag));

            NavView.Header =
                ((NavigationViewItem)NavView.SelectedItem)?.Content?.ToString();
        }
    }

标签: c#wpfxaml.net-core

解决方案


Navigated事件在其上Frame而不是在其上Content

ContentFrame.Navigated += On_Navigated;

推荐阅读