Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAUI project, cannot navigate to a new page

Tags:

c#

maui

I've ran into a bug in MAUI and I just wanted to know if I'm the only one or if I'm doing something wrong in this new environment :)

Before I've always been able to do this when navigating to a new page via a button click:

private async void OnButtonClicked(object sender, EventArgs e)
       {
           await Navigation.PushAsync(new Page1());
       } 

But it does not seem to work anymore, this is how my Page1 looks like in C# code:

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
           InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            await MainThread.InvokeOnMainThreadAsync(async () => { await LoadRecipes(); });
        }

        private async Task LoadRecipes() 
        {

           //no code yet
        }
    }
}

When I run this code and click the button, nothing happens.

like image 973
CodeBuddyBenny Avatar asked Nov 14 '22 19:11

CodeBuddyBenny


2 Answers

You can navigate using this in a ButtonClick or TapGestureRecongnizer.

private void TapGestureRecognizer_Tapped_2(object sender, EventArgs e)
    {
        App.Current.MainPage = new NavigationPage(new Page1());

    }

Or in a Button.

 private void Button_Clicked(object sender, EventArgs e)
    {
        App.Current.MainPage = new NavigationPage(new Page1());
    }
like image 141
Bas H Avatar answered Dec 26 '22 15:12

Bas H


Try this: First in your App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
}

Then in your MainPage you can do what you are attempiting to do:

private async void btnNav_Clicked(object sender, EventArgs e)
{
    await Navigation.PushAsync(new Page1());
}

This is supposed to work and to keep track of user's navigation, but in some weird scenarios im facing this is not working very well, but it's the solution that Microsoft documents on it's getting started guide. I would like to see more information in their Maui guide about navigation.

like image 36
Juan Ignacio Avendaño Huergo Avatar answered Dec 26 '22 13:12

Juan Ignacio Avendaño Huergo