What is best practice for doing this? Keep in mind that I am using the MVVM pattern and Windows Template studio. So if there is some built in solution/best practice for dataloading I would apprecihate to know about that.
I am loading data async from an API through http which takes a couple of seconds. So when a user clicks on the page he have to wait a bit before the data appears. I want the data to be preloaded before the user clicks the page. The page data should be loaded when application starts.
This is my code for loading data into the page right now:
XAML
<i:Interaction.Behaviors>
<ic:EventTriggerBehavior EventName="Loaded">
<ic:InvokeCommandAction Command="{x:Bind ViewModel.LoadedCommand}" />
</ic:EventTriggerBehavior>
</i:Interaction.Behaviors>
ViewModel:
public ICommand LoadedCommand => loadedCommand ?? (loadedCommand = new RelayCommand(LoadData));
private async void LoadData()
{
await LoadItemsAsync();
await LoadImagesAsync();
}
private async Task LoadItemsAsync()
{
var items = await itemsDataAccess.GetAllAsync();
foreach (Item i in items)
ItemImageLinks.Add(new ItemImageLink() { Item = i });
}
private async Task LoadImagesAsync()
{
if (!InternetConnectionService.IsConnected())
return;
foreach (ItemImageLink iml in ItemImageLinks)
iml.Image = await imagesDataAccess.GetImageAsync(iml.Item.ImageStringName);
}