.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,483 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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"?