WinUI NavigationView Question

Jassim Al Rahma 1,526 Reputation points
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 App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
727 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,111 Reputation points Microsoft Vendor
    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