internal void Refresh()
{
if (_isQuerying || Layer == null)
return; // already querying
if (!(Layer is GroupLayerBase)) // GroupLayer : don't wait for layer intialized, so the user will see the layer hierarchy even if the group layer is not initialized yet (else would need to wait for all sublayers initialized)
{
if (!Layer.IsInitialized)
{
IsBusy = true; // set busy indicator waiting for layer initialized
return; // Refresh will be done on event Initialized
}
LayerItems = null;
}
LegendItems = null;
var simplifiedLegendSupport = new SimplifiedLegendSupport(Layer);
var legendSupport = LayerItemsOptions.ReturnLegendItems == false && simplifiedLegendSupport.IsSupported ? simplifiedLegendSupport : Layer as ILegendSupport;
if (legendSupport != null)
{
IsBusy = true;
_isQuerying = true;
Action queryLegendInfosAction = delegate()
{
legendSupport.QueryLegendInfos(
// callback
(result) =>
{
if (LegendTree == null)
{
// The legend item has been detached ==> result no more needed
IsBusy = _isQuerying = false;
return;
}
if (result != null)
{
Description = result.LayerDescription;
if (string.IsNullOrEmpty(Label)) // Label is set with LayerID : keep it if not null
Label = result.LayerName;
Map map = LegendTree.Map;
// Convert scale to resolution
maxServiceResolution = result.MinimumScale == 0.0 ? double.PositiveInfinity : ConvertToResolution(result.MinimumScale, map);
minServiceResolution = ConvertToResolution(result.MaximumScale, map);
// Combine Layer and Service resolution
MinimumResolution = Math.Max(Layer.MinimumResolution, minServiceResolution);
MaximumResolution = Math.Min(Layer.MaximumResolution, maxServiceResolution);
IsHidden = result.IsHidden;
if (result.LayerLegendInfos != null)
{
LayerItems = result.LayerLegendInfos.Select(info => new LayerItemViewModel(Layer, info, Description, map)).ToObservableCollection();
}
if (result.LegendItemInfos != null)
{
LegendItems = result.LegendItemInfos.Select(info => new LegendItemViewModel(info)).ToObservableCollection();
}
}
// If groupLayer -> add the child layers
AddGroupChildLayers();
// Kml layer particular case : if a KML layer has only a child which is not another KML layer ==> set the child item as transparent so it doesn't appear in the legend
ProcessKmlLayer();
LegendTree.UpdateLayerVisibilities();
_isQuerying = false;
// Fire event Refreshed without exception
LegendTree.OnRefreshed(this, new Legend.RefreshedEventArgs(this, null));
if (_dispatcher != null)
_dispatcher.BeginInvoke(new Action(() => IsBusy = false));
else
IsBusy = false;
},
// errorCallback
(ex) =>
{
// Fire event Refreshed with an exception
if (LegendTree != null)
LegendTree.OnRefreshed(this, new Legend.RefreshedEventArgs(this, ex));
_isQuerying = false;
IsBusy = false;
}
);
};
if (_dispatcher != null)
_dispatcher.BeginInvoke(queryLegendInfosAction);
else
queryLegendInfosAction();
}
else
{
IsBusy = false;
MinimumResolution = Layer.MinimumResolution;
MaximumResolution = Layer.MaximumResolution;
// Fire event Refreshed
if (LegendTree != null)
LegendTree.OnRefreshed(this, new Legend.RefreshedEventArgs(this, null));
}
}