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.
Answers
If you google this there are a lot of hits:
https://github.com/luberda-molinet/FFImageLoading/issues/833
https://forums.xamarin.com/discussion/103653/does-anyone-know-how-replacestringmap-works-on-svgcachedimage
In that one the user asked mid-thread:
Then two posts later writes:
https://stackoverflow.com/questions/55108173/xamarin-forms-ffimageloading-svg-forms-setting-replacestringmap-after-object-in
Seems it's been implemented with some strict and poorly understood rigidity so you must follow precise order in what you do.
Yes, I have already visited those links (in fact, your 3rd link is my own SO question) but none of the suggestions worked for me. I assume there's a bug because
svg.ReloadImage();
should work but does not.