question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked RoyLi-MSFT commented

WinUI NavigationView Question

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
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please tell me where did you find these codes? Generally, we do navigation using the Frame.navigate(TypeName) in the ItemInvoked event. The TypeName is the name of a Page.



0 Votes 0 ·

1 Answer

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered RoyLi-MSFT commented

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When you say Page Object, do you mean the ContentPage? because I cannot find the Page in the New dialog box

202870-untitled.png


0 Votes 0 ·
untitled.png (22.7 KiB)
RoyLi-MSFT avatar image RoyLi-MSFT JassimAlRahma-9056 ·

It's a Page object for the WinUI3. You could search for Page in the Search Bar on the top right.

202993-image.png


0 Votes 0 ·
image.png (31.0 KiB)