I'm trying to obtain LegendInfo's from all of my Feature Layers in my map (after they're loaded and shown on the map). I've attempted to await on the Layer.GetLegendInfosAsync() using a couple of different methods, and the task status is always set to WaitingForActivation. I'm trying to place my Legends into a MAUI CollectionView. If I show the layer again in my ContentView, it works the next time I open that ContentView, but not the first time.
I've tried using the code in the LayerListDataSource (from the Legend Toolkit) as well as TocItem.cs (from the Table of Contents).
I might be missing some of the code located in LayerContentDataSource.cs (from Legend Toolkit) but not sure..
Methods follow:
//This is the version in LayerListDataSource.cs (tried with and without ConfigureAwait(false)
private async Task<IReadOnlyList<LegendInfo>> LoadLegendOrig(ILayerContent layer)
{
var result = await layer.GetLegendInfosAsync().ConfigureAwait(false);
if (result.Count > 0)
{
Debug.WriteLine($"TocViewModel.cs: LoadLegendOrig() result.Count > 0 = {result.Count}");
}
return result;
}
//This is the version in TocItem.cs
private async Task<IReadOnlyList<LegendInfo>> LoadLegend(ILayerContent lc)
{
var task = lc.GetLegendInfosAsync();
if (task.IsCompleted)
{
await System.Threading.Tasks.Task.Yield();
}
var infos = await task;
return infos;
}
This is fixed. I changed my methodology to handle the Task functionality using Task.WhenAll(legendTasks) instead, in order to wait for all the LegendInfos to get loaded prior to adding it to my observable collection.