Search code examples
c#xamluwpwin-universal-app

NavigationView.Loaded Method doesn't get called


I'm trying to use a Navigation View in my UWP application. I wrote events for Loaded, SelectionChanged and ItemInvoked. But it's like none of them gets called whatever I do.

This is my Navigation View in XAML

        <NavigationView x:Name="nvSample" 
                        Background="{ThemeResource NavigationViewDefaultPaneBackground}"
                        IsBackButtonVisible="Collapsed"
                        Loaded="nvTopLevelNav_Loaded"
                        SelectionChanged="nvTopLevelNav_SelectionChanged"
                        ItemInvoked="nvTopLevelNav_ItemInvoked">
            <NavigationView.MenuItems>
                <NavigationViewItem Icon="Home" Content="Home" Tag="Home" />
                <NavigationViewItem Icon="Flag" Content="Memory Palace" Tag="SamplePage2" FontFamily="Segoe UI" />
                <NavigationViewItem Icon="Accept" Content="Test Arena" Tag="SamplePage3" />
                <NavigationViewItem Icon="OtherUser" Content="Sophie" Tag="SamplePage4" />
            </NavigationView.MenuItems>
            <Frame x:Name="contentFrame" >  
            </Frame>
</NavigationView>

I wrote provided methods in MainPage.xaml.cs like this

        private void nvTopLevelNav_Loaded(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("in method");
            // set the initial SelectedItem
            foreach (NavigationViewItemBase item in nvSample.MenuItems)
            {
                if (item is NavigationViewItem && item.Tag.ToString() == "home")
                {
                    nvSample.SelectedItem = item;
                    break;
                }
            }
            contentFrame.Navigate(typeof(nvTop.home));
            Console.WriteLine("loaded bruh");
        }

        private void nvTopLevelNav_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
        {
            contentFrame.Navigate(typeof(nvTop.memory_palace));
            Console.WriteLine("selection changed");
        }

        private void nvTopLevelNav_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            contentFrame.Navigate(typeof(nvTop.memory_palace));
            Console.WriteLine("item invoked");
        }

Console prints for debug purpose only. In ItemInvoked and SelectionChanged methods I wrote a sample code just to check whether they work. I'll write it completely after. I think I provided all details. My problem is my methods for events doesn't get called. How do I correct thet error

Thanks in advance


Solution

  • This works on mine too. I thought this isn't working because I didn't saw any Console messages. So in UWP Console.writeln() doesn't work?

    According to your code snippet, you're developing a general UWP app. So, the answer is Yes. If you want to show some debug info in Output window, you could use System.Diagnostics.Debug.WriteLine("something...");.

    There's a special case that you could Create a Universal Windows Platform console app. If you're interested in it, you could see that document.