Reputation: 77
I'm trying to call an async function but when I try and do it it has the red line underneath it. I want the function to be called when the page is opened, thanks in advance.
public partial class Home : ContentPage
{
public class GoogleProfile
{
public string Id { get; set;}
}
public Home()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
await Check(/*What do i put in here*/);
}
public async Task Check(GoogleProfile googleprofile)
{
if (String.IsNullOrEmpty(googleprofile.Id))
{
}
else {
await Navigation.PushAsync(new LoginPage());
}
}
}
how would i call this? Sorry im new to C# and xamarin
Upvotes: 0
Views: 1948
Reputation: 11105
You should make your async calls in ContentPage.OnAppearing()
. The OnAppearing()
event will be called, as the name suggest, right when your page is being displayed. This is the expected behavior by the user. Also note that I changed your Check()
method to return Task
because, if you are able to edit the method signature, always try to change the return type of async methods from void
to Task
:
public partial class LoginPage : ContentPage {
public LoginPage() {
InitializeComponent();
}
protected override async void OnAppearing() {
await Check(/* Add code here to get your GoogleProfile object */);
}
public async Task Check(GoogleProfile googleprofile) {
var ID = googleprofile.Id;
if (string.IsNullOrEmpty(ID)) {
return;
} else {
await Navigation.PushAsync(new Home());
}
}
}
Technically, if you are dead set to not use OnAppearing()
you could do the Check()
before pushing your LoginPage
, though without seeing more code, that would seem like it would defeat the purpose of the LoginPage
.
Upvotes: 2