I can't change image source in xamarin on xaml.cs

44 views Asked by At

I want to change the Image source in xamarin but I can't see the value I gave to x:name in .cs somehow? .xaml :

 <Image Source="{local:ResourceResimExt ResourceId=OzelKalem.Content.images.yellowicon.png}"  x:Name="resim_icon"  VerticalOptions="CenterAndExpand" HorizontalOptions="Start" Margin="49,-28,0,0" IsVisible="true" WidthRequest="16" HeightRequest="16"/>

I want to write like this on xaml.cs:

resim_icon.Source = ImageSource.FromFile("redicon.png");

i tried it didn't work:

if ((KalanGun < 0 && durum == 1) && sonTarih < DateTime.Now)
{
   
    ResourceResimExt resource = new ResourceResimExt();
    resource.ResourceId = "OzelKalem.Content.images.redicon.png";
    var imageSource = ImageSource.FromResource("OzelKalem.Content.images.redicon.png");
    Xamarin.Forms.ImageSource.FromResource("OzelKalem.Content.images.redicon.png");
 
}
else if ((KalanGun > 0 && durum == 2) || durum == 2)
{
    ResourceResimExt resource = new ResourceResimExt();
    resource.ResourceId = "OzelKalem.Content.images.greenicon.png";
    var imageSource = ImageSource.FromResource("OzelKalem.Content.images.greenicon.png");
    Xamarin.Forms.ImageSource.FromResource("OzelKalem.Content.images.greenicon.png");
    //resim.ResourceId = "OzelKalem.Content.images.greenicon.png";


}

and xaml :<Image Source="{Binding ImageSource}"/> xaml.cs:

 private ImageSource _imageSource;
 public ImageSource ImageSource
    {
        get { return _imageSource; }
        set { _imageSource = value; }
    }
if ((KalanGun < 0 && durum == 1) && sonTarih < DateTime.Now)
{
_imageSource = ImageSource.FromResource("OzelKalem.Content.images.redicon.png");
}
1

There are 1 answers

0
Büşra Güler On

Actually there is something like embedded image structure in my project. But I couldn't understand how to change it dynamically in xaml.cs so I tried everything and it didn't work. Image source needs to be changed.

public class ResourceResimExt : IMarkupExtension
        {
            public string ResourceId
            {
                get;
                set;
            }
            public object ProvideValue(IServiceProvider serviceProvider)
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(ResourceId)) return null;
                    return Xamarin.Forms.ImageSource.FromResource(ResourceId);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }

just below this code is used like this:

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (string.IsNullOrEmpty(value.ToString()))
                return null;

            else if (value.ToString().Contains("Gelen"))
                return Xamarin.Forms.ImageSource.FromResource("OzelKalem.Content.images.callin.png");


            return Xamarin.Forms.ImageSource.FromResource("OzelKalem.Content.images.callout.png");
        }