user4661780
user4661780

Reputation:

Image doesn't show in Xamarin.forms (PCL)

I have an image withn name fb.png and it's in root project(prtable) and I add this image to Resource>drawble in Droid project.

Thees is my MainPage.xaml code:

 <Image x:Name="img1"></Image>

And Thees is my MainPage.xaml.cs code:

 public MainPage()
    {
        InitializeComponent();

        ImageSource img = ImageSource.FromResource("App2.fb.png");
        img1.Source = img;
        img1.Aspect = Aspect.AspectFit;
        img1.BackgroundColor = Color.Navy;

     }

What changed is need that image will be appeared?

Upvotes: 1

Views: 1267

Answers (2)

DavidS
DavidS

Reputation: 2934

If the file is saved in the Resources/Drawable directory, then you use FromFile, not FromResource. FromResource is used for images packaged as embedded resources in your built library.

You also need to specify the exact name of the file as it appears in Resources/Drawable, so this should do it:

public MainPage()
{
    InitializeComponent();

    ImageSource img = ImageSource.FromFile("fb.png");
    img1.Source = img;
    img1.Aspect = Aspect.AspectFit;
    img1.BackgroundColor = Color.Navy;

 }

Upvotes: 1

ClintL
ClintL

Reputation: 1453

Here is a full implementation of MVVM bound image resource to Image control. You need to set your viewmodel as the context of your page where the XAML is. Also accessing as "App2.fb.png" seems odd, it should just be fb.png. That might be a simpler fix.. just rename the image source to the exact name of the image as listed in Droid > resources

XAML

<Image
Aspect="AspectFit"
Source="{Binding PropertyImageStatusSource}">

Base ViewModel

Have your viewmodels inherit from a viewmodel base class so INotifyPropertyChanged is implemented on your accessors universally.

    public class _ViewModel_Base : INotifyPropertyChanged
    {


        //figure out what is getting set
        public virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);



            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        //attach handler with property name args to changing property, overridable in inheriting classes if something else needs to happen on property changed
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

ViewModel

Public MyViewModel : _ViewModel_Base 
{
    private string ImageStatusSource = "fb.png";
    public string PropertyImageStatusSource
    {
        set { SetProperty(ref ImageStatusSource, value); }
        get { return ImageStatusSource; }
    }

}

Upvotes: 0

Related Questions