'How can I set the Source property of a FFImageLoading.SvgCachedImage from a Dynamic Resource in a Xamarin C# view?

Here is is the XAMLcode I am currently using:

<ffimageloadingsvg:SvgCachedImage  Source="{DynamicResource GearIcon}" />

GearIcon is set to "Test.Resources.Theme.gear_light.svg" and everything works perfectly. When I set the Dynamic Resource to "Test.Resources.Theme.gear_dark.svg" then the image changes as expected.

Now I am trying to change this to use fluent C# but nothing is working when I do this:

var gear = new SvgCachedImage;
gear.DynamicResource(SourceProperty, SvgImageSource.FromResource(Application.Current.Resources["GearIcon"].ToString()));

I have also tried this:

gear.DynamicResource(SourceProperty, (Application.Current.Resources["GearIcon"].ToString()));

and

gear.DynamicResource(SourceProperty, (ImageSource)(Application.Current.Resources["GearIcon"].ToString()));

Does anyone have any idea how I can set the source dynamically when using C#. Nothing I have tried works so far except the XAML which I am not using right now.



Solution 1:[1]

The type of Source in SvgCachedImage is a ImageSource . So if you want to set it in code behind you could call the following line .

gear.Source = ImageSource.FromFile(Application.Current.Resources["GearIcon"].ToString());

Solution 2:[2]

using FFImageLoading.Svg.Forms;

SvgCachedImage gear = this.GetTemplateChild("YOUR SVGCACHEDIMAGE NAME ON YOUR XAML PAGE") as SvgCachedImage;

gear.source = SvgImageSource.FromResource("YOUR ROOT FOLDER NAME.YOUR IMAGE FOLDER NAME.GearIcon.svg");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas Zhang
Solution 2 Tyler2P