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

  • Question

  • User383110 posted

    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.

    Monday, March 11, 2019 6:30 PM

All replies

  • User268480 posted

    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:

    How should i use ReplaceStringMap to change color, for example, to red? Then two posts later writes: Perfects, it works now. Thanks

    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.

    Tuesday, March 12, 2019 5:10 PM
  • User383110 posted

    @utekai said: 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:

    How should i use ReplaceStringMap to change color, for example, to red? Then two posts later writes: Perfects, it works now. Thanks

    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.

    Wednesday, March 13, 2019 8:13 PM