Good afternoon. I am currently building an app with .Net Maui and .Net Framework 7.0 for Android and iOS (currently using the Pixel 5 Android Emulator), which should automatically do some stuff (such as authentication) on startup and then redirect the user accordingly to the corresponding page.
In my startpage (StartPage.xaml.cs) I am calling the initialization method from the viewmodel as follows:
protected override async void OnAppearing()
=> await _ViewModel.InitializeAsync();
While the initialization-code in the viewmodel looks like this:
public async Task InitializeAsync()
{
// ATTENTION: IF THE NEXT LINE IS COMMENTED OUT, EVERYTHING WORKS FINE.
// HOWEVER, INSTEAD OF THE DELAY THIS IS THE PLACE I WOULD LIKE TO INITIALIZE THE APP (CALL SERVER, AUTHENTICATE ETC).
await Task.Delay(1000);
var user = new User();
var parameters = NavigationService.GetParamsForObject(Tuple.Create("User", (object)user));
// Go to the main page
await NavigationService.GoToMainPage(parameters, true);
}
The app seems to start up correctly and the user gets redirected to the "main"-page as expected. However, as mentioned in the comment of the code-snippet above, the line "await Task.Delay(1000)" (where my actual initialization-logic would take place) causes a TaskCancelledException in the redirected MainPage (where a Map-Control with Custom-pins is loaded). If I comment out the line "await Task.Delay(1000)" everything works as expected.
Whats the problem with using an await-Statement in the InitializeAsync-Method? Am I missing something? Or is this just the wrong place to (automatically) initialize the app? I dont want the user to click on a button first, the initialization should take place automatically.
Thank you very much for your help.
I tried:
- MainThread.InvokeOnMainThread() to run the initialization-logic
- Using a Dispatcher
- Catch the TaskCancelledException, but this leads to unexpected behaviour
- Override OnNavigatedFrom instead of OnAppearing