question

DavidHoffman-6474 avatar image
0 Votes"
DavidHoffman-6474 asked DavidHoffman-6474 commented

Need to trigger a NavigationView.SelectionChanged event in code behind

I have a need to trigger a NavigationView.SelectionChanged event in code (C++) to take the user to a specific page that I already done in XAml. The Navigation for my app works just fine. I have several pages available on a Top nav bar, including Settings. Here is a pic.
142524-mainnavmenu.png


Here is the view of Settings nav bar.
142455-settingnavmenu.png

During start up, I check a dB to see if Factory Settings are present. If not, I want to take the user to that page. This happens on the main Page where I am showing a modal message box that they need to set Factory settings.

I followed the example from MS of how to code a complete Navigation for my UWP app, what I don't know how to do is make the navigation event happen.

Here are the functions I implemented for both the Main Page and Settings page. I can show my code, but it is working. Remember this an event I am trying to fire in code.

     void ContentFrame_NavigationFailed(IInspectable const& sender, NavigationFailedEventArgs const& args);
     void NavView_Loaded(IInspectable const& sender, RoutedEventArgs const& args);
     void NavView_SelectionChanged(IInspectable const& sender, NavigationViewSelectionChangedEventArgs const& args);
     void NavView_Navigate(wstring navItemTag, NavigationTransitionInfo const& transitionInfo);
     void NavView_BackRequested(NavigationView const& sender, NavigationViewBackRequestedEventArgs const& args);
     void CoreDispatcher_AcceleratorKeyActivated(CoreDispatcher const& sender, AcceleratorKeyEventArgs const& args);
     void CoreWindow_PointerPressed(CoreWindow const& sender, PointerEventArgs const& args);
     void System_BackRequested(IInspectable const& sender, BackRequestedEventArgs const& args);
     bool TryGoBack(void);
     void On_Navigated(IInspectable const& sender, NavigationEventArgs const& args);

Thank you for your help.

windows-uwpc++
mainnavmenu.png (42.4 KiB)
settingnavmenu.png (20.0 KiB)
· 3
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.



Maybe you should try something like this:

 auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
 rootFrame->Navigate( TypeName(SettingsPage::typeid), nullptr);


0 Votes 0 ·

I will give that a try, that is the second one, which seems to be C++.

0 Votes 0 ·

You gave me the clue I needed to make it work. It did take some trial and error to figure it out. Here is my solution.

    NavView_Navigate(L"settings", nullptr);

I made my code behind call this function, which is listed above, from an if statement where I asked the dB if it was configured for factory settings.

This causes the Settings page you see in one of the pics to be shown. But The settings Page normally loads the "About Page" so I added some code to select the Factory page when the Settings Page is loaded based on the dB not having the factory settings done. Here is that code in the UserSettingsNavView_Loaded().

     void SettingsPage::UserSettingsNavView_Loaded(IInspectable const& sender, RoutedEventArgs const& args){
         ContentFrame().Navigated({ this, &SettingsPage::On_Navigated });    
            
         if (dB.GetHasInitailConfiguration() == 1){
             UserSettingsNavView().SelectedItem(UserSettingsNavView().MenuItems().GetAt(1));
         }else{
             UserSettingsNavView().SelectedItem(UserSettingsNavView().MenuItems().GetAt(3));
         }    
            
         Window::Current().CoreWindow().Dispatcher().AcceleratorKeyActivated({ this, &SettingsPage::CoreDispatcher_AcceleratorKeyActivated });    
         Window::Current().CoreWindow().PointerPressed({ this, &SettingsPage::CoreWindow_PointerPressed });
         SystemNavigationManager::GetForCurrentView().BackRequested({ this, &SettingsPage::System_BackRequested });
     }

Thanks for the help.

0 Votes 0 ·

1 Answer

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

Hello,

Welcome to Microsoft Q&A!

If I understand you correctly, what you want to do is to navigate to a page via code. If it is, what you need is to set the NavigationView.SelectedItem property. You could set the target NavigationViewItem to the NavigationView.SelectedItem property. Then the NavigationView.SelectionChanged event will be triggered and the navigation will happen.

Like this:

   // Itme3 is a NavigationViewItem that I created in the Xaml
  nvSample().SelectedItem(Item3());

If your NavigationViewItem is not created in Xaml, you might need to find it in NavigationView.MenuItems Property

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.


· 1
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.

Please see my solution above. I am using Xaml. Thanks.

0 Votes 0 ·