Warm tip: This article is reproduced from stackoverflow.com, please click
visual-studio-2019 xamarin xamarin.android xamarin.forms xamarin.ios

Xamarin.Forms Images not showing

发布于 2020-04-13 10:48:06

I am developing a Cross-Platform app in Xamarin Visual Studio 2019 and having issues with image display.

I am trying to show an image with the following XAML code.

<StackLayout Grid.Row="0" Grid.Column="0" Spacing="0" >
    <Image
        HorizontalOptions="CenterAndExpand"
        VerticalOptions="CenterAndExpand"
        Source="logo"
        WidthRequest="{OnPlatform iOS=300, Android=250}">
    </Image>
</StackLayout>

Problem is image not displaying on either preview or deployed app.

I took references from multiple forums including help from StackOverflow but couldn't tackle the issue.

Here are the points and methods I worked on to resolve the issue.

  1. The file named logo is an android resource folder and is a proper .png file.
  2. Property for the file is set to AndroidResource.
  3. As per answers posted here, Xamarin Images not showing, file name does not have a hyphen in it, and also tried other posted answers on the same page.

  4. Image size 307X80 pixels so it's well within range for memory or size.

Android Drawable folder also contain default xamarin_logo.png image and that file works fine. But not newly added images. Is there anything I am missing here to add these image files to the solution?

Questioner
Mahadev
Viewed
47
Harikrishnan 2020-02-04 03:14

Either the image extension or image path or the build action for the image seems to be the problem in your case. I tried your code and got that working fine for both png and jpg images.

<StackLayout Spacing="0">
    <Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
            Source="github.png" WidthRequest="{OnPlatform iOS=300, Android=250}" />
    <Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
            Source="githubb.jpg" WidthRequest="{OnPlatform iOS=300, Android=250}" />
</StackLayout>

Note: I have removed the grid.row and grid.column indexing as I am not using my StackLayout inside a grid.

Please make sure whether you have kept your images directly inside the drawable folder in the Resources in Android. If not, you may have to give the Source name as FolderName\ImageName.Extension.

Refer below link for more details:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#local-images

Also, please ensure whether the BuildAction of your image is AndroidResource and Custom Tool is MSBuild:UpdateGeneratedFiles. Please refer to the below image.

enter image description here

I got the below output for the code which I posted above.

enter image description here

I hope that helps.