ThinkGeo.com    |     Documentation    |     Premium Support

LegendAdornmentLayer Bug

Hello,

I am currently facing an issue with the LegendAdornmentLayer. When I set the title to null, I encounter a “Object reference null” error.

To replicate the issue, follow these simple steps:

  1. Create a PrinterLayer.
  2. Create a LegendAdornmentLayer.
  3. Set the title to null.

I understand the cause of the bug, and I hope this information helps you. It seems that while calculating the width of the layer, the program tries to determine the maximum width of each LegendItem. However, it mistakenly continues to calculate the width of the title, even when it is set to null.

Thank you!

Hi Mahdi,

I couldn’t recreate the exception with the following code in the latest version:

        AddPrinterLayers();
    
        var legend = new LegendAdornmentLayer();
        legend.Title = null;
        MapView.AdornmentOverlay.Layers.Add(legend);

But I do see an internal exception that when a legend has no legend items and with its Title set to null, it still tries to create an image even its width/height is now 0. We’ve fixed it in beta 086, please pull the latest and have another try.

Thanks,
Ben

Hi Ben

I can see this is fixed in beta086 but I can see there are some other bugs which stoped me from updating to another version. So I have one question, when do you think we can have a stable release?

Best wishes
Mahdi

Hi Ben,

It appears that the bug still persists. Could you please check the LegendAdornmentLayer.GetWidthCore() method? This function calculates the maximum width for each LegendItem, and the error seems to occur when the Title property is set to null.

Here is the way to replicate the bug.

var legend = new LegendAdornmentLayer();
legend.Title = null;
var printerLayer = new LegendPrinterLayer(layer)

Best wishes,
Mahdi

Hi Mahdi,

I see it. And it’s been fixed in beta93.

We added some new features to the latest betas and aiming for releasing the next stable by May 1st, most likely it would be sometime around mid-April.

Thanks,
Ben

Hi Ben

Thanks for your kind message, really appreciate the bug has been fixed.

Best regards
Mahdi

No problem, Mahdi, just let us know whenever you have any questions.

Hi Ben

Upon retesting the bug in version beta093, I found that it still persists. In the relevant section of the code, the calculation of the Width incorrectly references the title instead of the legendItem, which should be reviewed and corrected.

    private float IiA_003D(LegendItem legendItem)
    {
        if (legendItem == null)
        {
            return 0f;
        }

        return title.Width + title.LeftPadding + title.RightPadding;
    }

    private float IyA_003D(LegendItem legendItem)
    {
        if (legendItem == null)
        {
            return 0f;
        }

        return title.Height + title.TopPadding + title.BottomPadding;
    }

Best wishes
Mahdi

Hi Mahdi,

You are right, it is a bug and have been fixed in beta095. Please pull and have another try.

Thanks,
Ben

Hi Ben

Thanks for fixing this bug, however now I face another issue which I can confirm I used to see it but it disappeared for some reason then in build beta097 I can see the issue again. The issue is related to footer when it is rendered outside for the Legend Box in the PrinterInteractiveOverlay.


Could you please let me know how I can fix this issue?
Here is the code I use for creating legends:

var layer = new LegendAdornmentLayer { Location = AdornmentLocation.UpperLeft };

items.ToList().ForEach(c =>
{
    var fontDrawingStyle = DrawingFontStyles.Regular;

    fontDrawingStyle = c.TextStyle.Font.IsBold ? fontDrawingStyle | DrawingFontStyles.Bold : fontDrawingStyle;
    fontDrawingStyle = c.TextStyle.Font.IsItalic ? fontDrawingStyle | DrawingFontStyles.Italic : fontDrawingStyle;
    fontDrawingStyle = c.TextStyle.Font.IsStrikeout ? fontDrawingStyle | DrawingFontStyles.Strikeout : fontDrawingStyle;
    fontDrawingStyle = c.TextStyle.Font.IsUnderline ? fontDrawingStyle | DrawingFontStyles.Underline : fontDrawingStyle;

    var font = new GeoFont(c.TextStyle.Font.FontName, c.Size, fontDrawingStyle);

    layer.LegendItems.Add(new CustomLegendItem
    {
        ImageStyle = c.ImageStyle,
        TextStyle = new TextStyle(c.TextStyle.TextColumnName, font, c.TextStyle.TextBrush),
        Size = font.Size,
        NeedImageStyle = c.NeedImageStyle
    });
});

if (title != null)
    layer.Title = title;

if (footer != null)
    layer.Footer = footer;

return new LegendPrinterLayer(layer)
{
    BackgroundMask = AreaStyle.CreateSimpleAreaStyle(GeoColors.WhiteSmoke,
        GeoColors.Black, 1),
};

Best wishes
Mahdi

Hi Mahdi,

This has been fixed in the latest beta102, please pull the latest and have a try.

Thanks,
Ben