Get user data before downloading the application

валера карманов 141 Reputation points
2024-10-04T07:06:07.21+00:00

I'm trying to add a login page to an Android app. There are no problems with the page itself, the issue is how to get user data before loading the app, namely.

public partial class App : Application
{
	public App()
	{
		InitializeComponent();
		var userId = Preferences.Get("userId", "0");
		if(Int32.Parse(userId) > 0)
		{
			HttpRequest request = new HttpRequest();
			request.LoadedUserData(userId, this);
		}
		else
		{
			MainPage = new Pages.LoginPage();
		}
		// System.NotImplementedException:
		// 'Either set MainPage or override CreateWindow.'
	}
}

public async void LoadedUserData(string userId, Application app)
{
	try
	{
		response = await client.GetAsync("/userdata.php?id=" + userId);
		if (response.IsSuccessStatusCode)
		{
			var content = await response.Content.ReadAsStringAsync();
			AppShell.userData = JsonSerializer.Deserialize<UserData>(content, jsonOptions);
			app.MainPage = new AppShell();
		}
		else
		{
			app.MainPage = new Pages.ErrorPage("Error text default");
		}
	}
	catch (Exception ex)
	{
		Debug.WriteLine("Error : " + ex.Message);
		app.MainPage = new Pages.ErrorPage(ex.Message);
	}
}

The error occurs in the "AppShell" class, most likely because the application continues loading regardless of whether the request is completed or not. How can I fix this?

Maybe it is possible to make an HttpClient request without using "Async"?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,483 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.