I am working on an automated screengrab tool. The tool is intent on automatically panning and zooming to a RasterLayer and then setting its IsVisible to true (while all other layer's visibility is set to false). The tool then snaps a picture of the UI and saves it to disk. This repeats for however many RasterLayers are on the map.
The issue I'm having is it is hard to tell when the map is fully done loading.
I created a method to wait for the DrawStatus to be 'Completed', but this doesn't seem to include the "…" loading indicator on the map. Using this method, my pictures have the fully in-focus and loaded Raster in them, but it still shows the loading indicator on the map in the image.
I can add an extra Task.Delay(300), but this is quite flimsy.
My "wait for draw status to be completed" method looks something like this:
public async Task<bool> TryWaitForRenderCompleteAsync(this GeoView geoView)
{
if (geoView.DrawStatus == DrawStatus.Completed)
return true;
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
EventHandler<DrawStatusChangedEventArgs> handler = (sender, e) =>
{
if (geoView.DrawStatus == DrawStatus.Completed)
tcs.TrySetResult(true);
};
geoView.DrawStatusChanged += handler;
try
{
if (geoView.DrawStatus == DrawStatus.Completed)
return true;
Task timeoutTask = Task.Delay(2000);
Task completedTask = await Task.WhenAny(tcs.Task, timeoutTask);
return await tcs.Task;
}
finally
{
geoView.DrawStatusChanged -= handler;
}
}
My execution code looks something like this:
public async Task SnapRastersAsync(MapView mv, string outDir, Func<string, Task> capture)
{
if (mv.Map == null)
return;
List<RasterLayer> rasterLayers = mv.Map.OperationalLayers.OfType<RasterLayer>().ToList();
foreach (RasterLayer rl in rasterLayers)
rl.IsVisible = false;
foreach (RasterLayer rl in rasterLayers)
{
rl.IsVisible = true;
await mv.ZoomToLayer(rl);
await mv.TryWaitForRenderCompleteAsync();
await capture(Path.Combine(outDir, rl.Name + ".png"));
rl.IsVisible = false;
}
}
Is there a more direct way to wait for the map to finish rendering?
P.S. I don't use GeoView.ExportImageAsync() because I have other controls overlayed on top of the map such as a scalebar, inset map, and sometimes popups. These are all excluded when just using ExportImageAsync.