WinUI NavigationView Question

Anonymous
2022-05-16T17:56:43.663+00:00

Hi,

I have a question please. I have the following NavigationView's ItemInvoked:

private void NavigationViewControl_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
{
switch (sender.MenuItems.IndexOf(args.InvokedItemContainer))
{
case 0:
sender.Content = new Patient();
break;

    case 1:
        // sender.Content = new Patient();
        break;

    case 2:
        // sender.Content = new Patient();
        break;

    default:
        break;
}

}

What type is Patient file? is it a ContentPage, ConteView, Page, Window, etc?

Thanks,
Jassim

Windows development | Windows App SDK
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-05-17T08:48:51.02+00:00

    Hello,

    Welcome to Microsoft Q&A!

    what is the best way to do it?

    Actually, the poster of the question that you shared already shows the correct way to navigate using NavigationView. You could put a Frame Object into NavigationView's content as the root frame. Then when the NavigationView item is clicked, call the Frame.navigate() to navigate to the target page.

    A simple code snippet looks like this

    Xaml

       <NavigationView ItemInvoked="NavigationView_ItemInvoked">  
                <NavigationView.MenuItems>  
                    <NavigationViewItem Icon="Home" Tag="HomePage"/>  
                    <NavigationViewItem Icon="Delete" Tag="DeletePage"/>  
                    <NavigationViewItem Icon="Save" Tag="SavePage"/>  
                    <NavigationViewItem Icon="Settings" Tag="SettingsPage"/>  
                </NavigationView.MenuItems>  
            Frame x:Name="rootFrame"/>  
            </NavigationView>  
    

    Code behind

      private void NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)  
            {  
                var navItemTag = args.InvokedItemContainer.Tag.ToString();  
                switch (navItemTag)   
                {  
                    case "HomePage":  
                        rootFrame.Navigate(typeof(HomePage));  
                        break;  
      
                    case "DeletePage":  
                        rootFrame.Navigate(typeof(DeletePage));  
                        break;  
      
                    case "SavePage":  
                        rootFrame.Navigate(typeof(SavePage));  
                        break;  
      
                    case "SettingsPage":  
                        rootFrame.Navigate(typeof(SettingsPage));  
                        break;  
                }  
            }  
    

    Besides, all of the pages like HomePage are Page objects.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.