Ganeshgm7
Ganeshgm7

Reputation: 455

How can I use async Method in Xamarin Forms when App starts?

I need to know How can I use async Method in Xamarin Forms when App starts? I need to show front page based on the condition.

public App()
        {
            InitializeComponent();

            if (SaveCredential.IpAddress == string.Empty || SaveCredential.PortNo == string.Empty)
            {
                MainPage = new NavigationPage(new Dhoni.IpDetail());
            }

            else if (SaveCredential.IpAddress != string.Empty && SaveCredential.PortNo != string.Empty)
            {
                if (await LoginPage.ConnectionCheck())

                {
                    if (SaveCredential.UserName != string.Empty && SaveCredential.Password != string.Empty)
                    {
                        if (await LoginPage.PasswordCheck(SaveCredential.UserName, SaveCredential.Password))
                        {
                            MainPage = new NavigationPage(new Dhoni.Dashboard());
                        }
                        else
                        {
                            MainPage = new NavigationPage(new Dhoni.LoginPage());
                        }
                    }
                    else if (SaveCredential.UserName == string.Empty || SaveCredential.Password == string.Empty)
                    {
                        MainPage = new NavigationPage(new Dhoni.LoginPage());
                    }

                }
                else
                {
                    MainPage = new NavigationPage(new Dhoni.IpDetail());
                }
            }
        }

I get error in these lines

if (await LoginPage.ConnectionCheck())

if (await LoginPage.PasswordCheck(SaveCredential.UserName,SaveCredential.Password))

Error is

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'.

Anyone have solution for this??

Upvotes: 3

Views: 2840

Answers (4)

Nicoara
Nicoara

Reputation: 398

You can do this

Task.Run(async () =>
{
    //--- async code ---
    Device.BeginInvokeOnMainThread(() => {
        //--- update UI ---
    });
});

when you call Task.Run() a new thread is created and you can call await in that thread and after the async method is done call Device.BeginInvokeOnMainThread() to update the UI because only the main thread can update UI.

Also you can use activity indicator to show the user that the data is loading.

Upvotes: 0

Daniel Luberda
Daniel Luberda

Reputation: 7454

If you really need to do that (but i advice not to):

YourMethodAsync.GetAwaiter().GetResult();

Upvotes: 0

Csharpest
Csharpest

Reputation: 1268

You can't use async/await in the ctor, read through async/await on msdn.

You have two options, you either override OnStart or OnResume (theres also OnSleep) and set them to async:

protected override async void OnStart() { //await ... }

or you set the App.Current.MainPage in the ctor of App.xaml.cs and then await your logic in the override asnyc void OnAppearing() of the page.

Upvotes: 1

Jason
Jason

Reputation: 89214

    protected async override void OnStart()
    {
        base.OnStart();

        // call your async method here
    }

Upvotes: 7

Related Questions