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

Alex 1 Reputation point
2022-06-22T20:04:03.9+00:00

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://learn.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.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2022-06-23T03:21:04.48+00:00

    Hello, @Alex

    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.

    0 comments No comments