Home > Enterprise >  Why is .NET MAUI crashing upon an event handler being activated?
Why is .NET MAUI crashing upon an event handler being activated?

Time:12-02

I am having a strange issue with my .NET MAUI app connected to an API controller and SQL backend. The app successfully accesses the login endpoint of my API and denotes that it logs in (I've confirmed by looking at my SQL db as well as by using Postman). However, once I hit the "login" button on my login page and I am notified that I have logged in, I thereafter call (within the same button event handler) absolute navigation to the home page using

await Shell.Current.GoToAsync($"{nameof(HomePage)}");

I use absolute shell navigation many other places in my app without issue (though to different pages), but for some reason, on this page, hitting "login" crashes my app immediately. Despite no exception being thrown or error, I have deduced that it is this line doing something wrong... and the app works fine if I remove this line. HomePage is a brand new page and I did register its route in appshell. I am flabergasted and would appreciate you input!

I expect to be able to use absolute shell navigation using .NET MAUI without my app crashing.

CodePudding user response:

A simple await Shell.Current.GoToAsync($"{nameof(HomePage)}"); in the button clicked event will not crash the app, unless you didn't register the Login Page in the AppShell. In other words, the code in your App.cs is such as:

public partial class App : Application
{
      public App()
      {
            InitializeComponent();
            MainPage = new LoginPage();
           // MainPage = new AppShell();
      }
}

If so, you can register the LoginPage in the shell and use MainPage = new AppShell(). Or you can try to change Shell.Current.GoToAsync($"{nameof(HomePage)}"); in the button clicked event as App.Current.MainPage = new AppShell().

In addition, if I'm wrong, could you please show more code about the Button click event and the shell?

  • Related