Hello,
Please do not invoke async/await method in the constructor.
You can create an interface like following code.
/// <summary>
/// Marks a type as requiring asynchronous initialization and provides the result of that initialization.
/// </summary>
public interface IAsyncInitialization
{
/// <summary>
/// The result of the asynchronous initialization of this instance.
/// </summary>
Task Initialization { get; }
}
And initialization is started in the constructor (when we call InitializeAsync) like edited following ShoppingCartYViewModels.cs
code.
public partial class ShoppingCartYViewModels : ObservableObject, IAsyncInitialization
{
[ObservableProperty]
ObservableCollection<Plan> planY;
public Task Initialization { get; private set; }
public ShoppingCartYViewModels()
{
Initialization = InitializeAsync();
}
private async Task InitializeAsync()
{
// Asynchronously initialize this instance.
string filePath = "sabafon_data.json";
var carriers = await FileAssetHelper.ReadJsonFileAsync(filePath);
PlanY = new ObservableCollection<Plan>(carriers["sabafon"].card.ToList());
}
}
Here is a document about Task asynchronous programming model, you can refer to.
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.