Select to view content in your preferred language

Unable to get GetLegendInfosAsync() to work against my maps layers

333
1
01-18-2024 04:08 PM
KarenRobine1
New Contributor III

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;

}

0 Kudos
1 Reply
KarenRobine1
New Contributor III

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. 

0 Kudos