Hikari
Hikari

Reputation: 599

Xamarin asyc await task

I would like to execute an async method inside the main function of a partial class in Xamarin.

namespace SgatMobile
{
    [XamlCompilation(XamlCompilationOptions.Compile)]

    public parial class myClass
    {
        public myClass()
        {
            InizializeComponent();
            ViewModel.ShowLoading = true; // This show the loading

            // How can I call myAsyncMethod and wait 5 seconds before hiding the loading?
            // If i call directly myAsyncMethod() it's run in synchronous.
            // If I call it using await (await myAsyncMethod();), Visual Studio throw this build error: 
            // The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

            ViewModel.ShowLoading = false; // This hide the loading
        }

        public async Task<bool> myAsyncMethod()
        {
            await Task.Delay(5000); 
            return true;
        }
    } 
} 

How can I call myAsyncMethod and wait the 5 seconds of Task.Delay?

Upvotes: 1

Views: 63

Answers (1)

divyang4481
divyang4481

Reputation: 1793

use OnAppearing method to call your async method

Upvotes: 2

Related Questions