Forum Libraries, Components, and Plugins
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

FFImageLoading.Svg.Forms: Setting ReplaceStringMap after object initialization doesn't do anything.

AaronTAaronT Member ✭✭

Using Xamarin.FFImageLoading.Svg.Forms I have created a custom class RecolorableSvg that inherits from SvgCachedImage with a bindable property NewColor that changes colors in the SVG by setting the ReplaceStringMap. Unfortunately, it only works if I set the NewColor before the RecolorableSvg object initializes.

Setting NewColor to an explicit color (e.g. XAML: NewColor="Red") will successfully recolor the SVG, but setting NewColor via a XAML binding or even directly via code that runs after the the RecolorableSvg has been created does not cause the SVG to be recolored.

Here is the method that is called in RecolorableSvg when NewColor is set:

private static void OnNewColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
  var svg = (RecolorableSvg)bindable;

  var newColor = (Color)newValue;

  string oldColorText = svg.OriginalColorText;
  if ((Color)oldValue != Color.Default)
    oldColorText = ((Color)oldValue).GetHexString();

  Console.WriteLine($"'{oldColorText}' --> '{newColor.GetHexString()}'");

  svg.ReplaceStringMap = new Dictionary<string, string>
    { $"fill=\"{oldColorText}\"", $"fill=\"{newColor.GetHexString()}\"" }

After setting ReplaceStringMap I have tried adding svg.ReloadImage(); and svg.OnPropertyChanged(nameof(FFImageLoading.Forms.CachedImage.SourceProperty));, but the only way that I can get it to change colors is by explicitly setting svg.Source to a URI string.


Sign In or Register to comment.