Skip to content

SvgImage Exception when setting SvgPath Property late #60

@franzdevel

Description

@franzdevel

I've run into exceptions when instantiating an SvgImage as follows:

sampleButton = new SvgImage
{
    IsVisible = false,
    SvgAssembly = typeof(App).GetTypeInfo().Assembly,
    HeightRequest = 44,
    WidthRequest = 44,
};

And, at a later point in time:

sampleButton.IsVisible = true;
sampleButton.SvgPath = "MyPath.MyImage.svg";

The SvgImage.RenderSvgToCanvas() method is invoked even though the SvgImage is initially invisible. The method fails because LoadedGraphic is null:

public IImageCanvas RenderSvgToCanvas (...)
{
    var originalSvgSize = LoadedGraphic.Size; // Exception because LoadedGraphic = null
    var finalCanvas = createPlatformImageCanvas (outputSize, finalScale);
    ...
}

A possible solution might be:

public IImageCanvas RenderSvgToCanvas (...)
{
    var finalCanvas = createPlatformImageCanvas(outputSize, finalScale);
    if (LoadedGraphic == null)
    {
        return finalCanvas;
    }
    var originalSvgSize = LoadedGraphic.Size;
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions