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,489 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 42,431 Reputation points Microsoft Vendor
    2024-10-07T05:42:06.59+00:00

    Hello,

    For the case of obtaining user information to determine the page display, it is recommended that you place it in the OnAppearing method of AppShell for the following reasons.

    • App.xaml.cs is not a UI component. For UI initialization, it would be a better choice to write it in AppShell.xaml.cs.
    • HttpClient initiates a request to the Service asynchronously, so it is necessary to use await when calling it, but the async/await keywords are not allowed in the constructor. Therefore, you can put this method in the OnAppearing method of AppShell.xaml.cs to process this request when the UI is initialized.
    protected override async void OnAppearing()
    {
        base.OnAppearing();
        // Handle Http request here.
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.