'c# calling async function from constructor gives green underline in VS
say I am doing something async and calling this function from the class constructor:
public partial class Page_MainMenuDetail : TabbedPage
{
ObservableCollection<AdsType> mainList = new ObservableCollection<AdsType>();
public Page_MainMenuDetail()
{
InitializeComponent();
LoadMainContent(false);
}
private async Task LoadMainContent(bool loadMore)
{
if (!loadMore)
{
mainList = await ReloadAdapter.LoadAllAds(0, 4);
listview_mainView.ItemsSource = mainList;
}
}
}
My reloadAdapter is just a class that makes an API call and returns an observable collection. But since I await for the reload adapter result, the LoadMainFunction() function has to be async. But that means, that when I call it from inside the constructor it looks like this in the framework (VS):
The green line indicates that the function is not beeing awaited. Which is true, but I cannot await inside a constructor (as you cannot make a constructor async).
Is this my wrongdoing or is this a common thing?
Ofc I can put the LoadMainContent() into another function that itself is async nbut not a task, and then call that from the constructor, but that is just a workaround to get rid of the green underline.
Or maybe I am doing this concept wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

