Home > Blockchain >  App can't Navigate to Page1 ('MainPage does not contain a definition for 'RootFrame_O
App can't Navigate to Page1 ('MainPage does not contain a definition for 'RootFrame_O

Time:06-24

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.

https://i.stack.imgur.com/XsVDJ.png

CodePudding user response:

Based on the error message, it means 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. So you are getting this error.

How to solve it

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

<Frame x:Name="rootFrame"/>

Option two, 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)
    {

    }
  • Related