Home > other >  Why Image not loading on android - xamarin
Why Image not loading on android - xamarin

Time:12-18

I put crypto.png image into drawable folder under Resources in Android Project.

After I right click on the image -> Build Action -> Embedded Resource.

In MainPage.xaml on the main project I try to load the image like that:

<Image x:Name="HeadImage"
           WidthRequest="100"
           HeightRequest="100"
           MinimumHeightRequest="100"
           MinimumWidthRequest="100"
           VerticalOptions="CenterAndExpand"
           HorizontalOptions="CenterAndExpand"
           Aspect="AspectFit"
           Source="crypto.png"/>

But the image not load.

I try a second method like this in the c# code:

var HeadImage = new Image { Aspect = Aspect.AspectFit };
HeadImage.Source = ImageSource.FromFile("crypto.png");

And this method not worked again..

Build Action

CodePudding user response:

You need to let your image in drawable folder as Android Resource instead of Embedded Resource, and to use it, you need to:

HeadImage.Source = "crypto.png";

Also, before set the image, is a good practice to remove MinimumWidthRequest and MinimumHeightRequest. This is safe ONLY if you are sure the image have this minimum size. Otherwise, your image will not appear.

To know more about image in Xamarin, see here.

And to understand the difference between the ways of set an ImageSource see here.

  • Related