0

So the simple idea is that I have SVG data as a string that I fetch from Internet. I would like to show that as an icon in my app. Is there any way I can do this?

I have seen countless examples where SVG data is in a file located in the app's directory that is then showed but this is not what I am looking for. I literally have the data in XML format after http request and I only need to transform that to a Image or something else visible on the screen.

I have been trying to find a solution to this for hours now, so I would really appreciate some help :S

3
  • learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/… Commented Aug 3, 2021 at 21:15
  • 1
    or use FFImageLoading SvgImageSource.FromSvgString Commented Aug 3, 2021 at 21:21
  • Thanks for the recommendation @Jason ! SvgImageSource.FromSvgString did the trick really well. Great point in this method is also that you can first make changes to your string before you actually pass it forward (for example change colors etc). Commented Aug 5, 2021 at 15:52

1 Answer 1

1

Android doesn't support svg with ImageView directly,you could display SVG with some commonly used third-party controls.

Like FFImageLoading.

Add Xamarin.FFImageLoading.Svg nuget package to platform project. Use ImageService with svg data resolver for displaying svg images.

For Android:

<ImageView
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

then use like:

var svgString = @"<svg><rect width=""30"" height=""30"" style=""fill:blue"" /></svg>";
ImageView imageView = FindViewById<ImageView>(Resource.Id.image_view);
ImageService.Instance
    .LoadString(svgString)
    .LoadingPlaceholder(placeHolderPath)
    .WithCustomDataResolver(new SvgDataResolver(64, 0, true))
    .WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(64, 0, true))
    .Into(imageView);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for help! I ended up using this exact library after I continued my research the next day. I used SvgImageSource.FromSvgString in my final solution.
how to achieve PCL in xamarin forms.
XML file to SVG image format dynamically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.