question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReplaceStringMap doesn't work

See original GitHub issue

I’d like to replace a color within an SVG file.

As far as I found out, ReplaceStringMap is the way to go.

The following example should display a blue rectangle, but it’s red (as defined in the SVG file):

The SVG file contains a red rectangle:

<svg viewBox="0 0 24 24">
  <rect fill="#ff0000" width="24" height="24" />
</svg>

Code to display the image - note the ReplaceStringMap should replace red by blue:

  public partial class MainPage : ContentPage {
    public MainPage() {
      var layout = new StackLayout();

      layout.Children.Add(new SvgCachedImage {
        Source = "resource://Foo.Images.test.svg",
        ReplaceStringMap = new Dictionary<string, string> { { "ff0000", "0000ff"} },
        WidthRequest = 98, HeightRequest = 98
      });

      this.Content = layout;
    }

The result is a red rectangle instead of a blue one. Am I doing something wrong?

(PS.: I also tried calling ReloadImage() but that didn’t help either)

Xamarin.FFImageLoading.Svg version is 2.2.25.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
stmax82commented, Nov 30, 2017

Yes all projects use the same version.

It looks like the order in which the properties are set matters:

This one does not work (image stays red):

      layout.Children.Add(new SvgCachedImage {
        Source = "resource://ReplaceStringMapProblem.test.svg",
        ReplaceStringMap = new Dictionary<string, string> { { "ff0000", "0000ff" } },
        WidthRequest = 98, HeightRequest = 98
      });

This one works (image turns blue):

      layout.Children.Add(new SvgCachedImage {
        ReplaceStringMap = new Dictionary<string, string> { { "ff0000", "0000ff" } },
        Source = "resource://ReplaceStringMapProblem.test.svg",
        WidthRequest = 98, HeightRequest = 98
      });

So it seems like the replacement does not work when the ReplaceStringMap property is set after the Source property.

0reactions
andreysilovcommented, Jul 26, 2022

For me it works on both platforms (iOS and Android):

if (Source is SvgImageSource source)
{
    var colorMap = new Dictionary<string, string> { { "regex:fill=\"#(?:[0-9a-fA-F]{3}){1,2}\"", $"fill=\"{color.ToHex()}\"" } };
    source.ReplaceStringMap = colorMap;
    ReplaceStringMap = colorMap;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

FFImageLoading Xaml ReplaceStringMap SvgCachedImage
Hello Xamarin,. I would like to change text property of an SVG image at runtime. I use FFImageLoading library on a forms application....
Read more >
Does not work Regex for multiple string split in java?
I want to split string by "[11]" , "[86]" , "[87]" Tried following code but does not work. void testSplit() { StringBuilder message...
Read more >
Stream::Replace Will Not Work
I am able to cause failures by replacing with STREAM::expression but, again, any replacement attempts with STREAM::replace will not work.
Read more >
ounit2 2.2.3 · OCaml Package
to rename oUnit to ounit was not working on Windows and MacOSX because their filesystems are case insensitive and the ... replace String.map...
Read more >
FFImageLoading - Fast & Furious Image Loading changelog
StartNew for each task (not for batch of tasks as previously) ... This was really important to me, as it will allow to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found