Is there a way I can do a Xamarin ContentPage Async OnCreate method?

CCant 1 Reputation point

Hi, I dynamically create the pages using the constructor of the ContentPage rather than using xaml. As part of this I sometimes need to call async methods which obviously doesn't follow the "Async all the way" theory as the constructor cannot be async.

So effectively what I want to do is an async OnCreate() method (OnAppearing doesn't work as I have to be able to navigate away and back again without recreating the screen) but content pages don't appear to have that method.

If I then call an async method in the constructor I either need to .Wait() it which freezes the UIThread which is bad, the alternative is to have it just 'fire and forget' which doesn't really work as the page will initialise before I have refreshed the data.

Am I missing something?

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,820 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Alessandro Caliaro 2,796 Reputation points

    I usually use Task.Run(async....

  2. JessieZhang-MSFT 7,516 Reputation points


    Welcome to Microsoft Q&A!

    It is recommended you could read through document: .

    Specifically: .

    So we should void calling .Wait() or .Result , as this is going to lock our app.

    And we don't recommend spin up a new Task either, just call the ContinueWith, but realize that your page may be done, so check your state:

            t =>   
        if (t.IsFaulted)   

    Refer : .

    There are some similar threads about this, you can check it here :

    If the response is helpful, please click "Accept Answer" and upvote it.
    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.

    No comments