question

Alex-8696 avatar image
0 Votes"
Alex-8696 asked NicoZhu-MSFT commented

App can't Navigate to Page1 ('MainPage does not contain a definition for 'RootFrame_OnNavigated' and no accessible...)

I've tried searching for solutions and couldn't find any accurate ones. I keep getting the same error when launching my UWP/XAML app:

 CS1061  'MainPage' does not contain a definition for 'RootFrame_OnNavigated' and no accessible extension method 'RootFrame_OnNavigated' accepting a first argument of type 'MainPage' could be found


I've gone through the documentation and changed

 rootFrame.Navigate(typeof(MainPage), e.Arguments);

to

 rootFrame.Navigate(typeof(Page1), e.Arguments);

in order to specify Page1 in the call to Frame.Navigate instead of MainPage. (https://docs.microsoft.com/en-us/windows/apps/design/basics/navigate-between-two-pages)

Here is the code in MainPage.xaml:

 <muxc:NavigationView x:Name="NavigationViewControl"
         IsTitleBarAutoPaddingEnabled="False"            
         IsBackButtonVisible="Visible"           
         Header="Home" 
         DisplayModeChanged="NavigationViewControl_DisplayModeChanged"
         SelectionFollowsFocus="Enabled"
         ItemInvoked="NavigationView_OnItemInvoked"
         PaneDisplayMode="Auto"
         Canvas.ZIndex="0">
         <muxc:NavigationView.MenuItems>
             <muxc:NavigationViewItem Icon="Home" Content="Home" x:Name="Menu1Item" Tag="Page1"/>
             <muxc:NavigationViewItem Icon="Contact" Content="Account" x:Name="Menu2Item" Tag="Page2"/>
             <muxc:NavigationViewItem Icon="Bullets" Content="Absences" x:Name="Menu3Item" Tag="Page3"/>
             <muxc:NavigationViewItem Icon="Library" Content="Grades" x:Name="Menu4Item" Tag="Page4"/>
             <muxc:NavigationViewItem Icon="Flag" Content="Grad Reqs" x:Name="Menu5Item" Tag="Page5"/>
         </muxc:NavigationView.MenuItems>
         <Grid Padding="20">
             <Frame x:Name="rootFrame" Navigated="RootFrame_OnNavigated"/>
         </Grid>
     </muxc:NavigationView>
 </Grid>

MainPage.xaml.cs:

 private void NavigationView_OnItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
     {
         FrameNavigationOptions navOptions = new FrameNavigationOptions();
         navOptions.TransitionInfoOverride = args.RecommendedNavigationTransitionInfo;
    
         string navItemTag = args.InvokedItemContainer.Tag.ToString();
    
         Type pageType = null;
         if (navItemTag == "Page1")
         {
             pageType = typeof(Page1);
         }
         else if (navItemTag == "Page2")
         {
             pageType = typeof(Page2);
         }
         else if (navItemTag == "Page3")
         {
             pageType = typeof(Page3);
         }
    
         if (pageType == null)
         {
             return;
         }
    
         rootFrame.NavigateToType(pageType, null, navOptions);
     }

I don't see any missing references or definitions, so I'm confused. I made sure spelling and casing was correct.

windows-uwp
· 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.

@Alex-8696, Does the following answer work, and do you have any updates for this thread ? If the answer is the right solution, please click "Accept Answer" and kindly upvote it.

0 Votes 0 ·

1 Answer

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered NicoZhu-MSFT edited

Hello, @Alex-8696

Welcome to Microsoft Q&A!

Derive from the error info, it looks that you've declared an event handler for the Frame.Navigated event. But the compiler doesn't find that event handler in the MainPage.xaml.cs code behind, then you are getting this error.

Solution

if you are not trying to handle the event, the easy way is to change the code of the Frame like this:

 <Frame x:Name="rootFrame"/>

if you want to handle this event, you will need to add the event handler in the MainPage.xaml.cs like this:

     private void RootFrame_OnNavigated(object sender, NavigationEventArgs e)
     {

     }


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.



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.