Xamarin Forms image not showing

我与影子孤独终老i 提交于 2019-12-04 00:00:43

问题


I have a Xamarin Forms (2.0) Android app where I'm trying to show an image. I've got an icon called icon-pages-r1.png which I'm trying to show using the following code:

<Image Source="icon-pages-r1.png" />

The image isn't showing though. When I change the source to Icon.png (the default Xamarin icon) it does work.

The image is a semi-transparent PNG (thus a colored icon in the middle and transparent around it), it's 46x46 and in Windows it shows fine as item type PNG File. I tried opening the image in Paint and re-saving it (which kills the transparency) but that doesn't work either. The Build Action for the images are AndroidResource with Copy to Output Directory set to Do not copy.

Does anyone know why I can't get this image to show in my app?


回答1:


You can't use hyphens in image names for Xamarin Android. Get rid of the hyphens (in both the file name and the Image reference) and you'll be set.




回答2:


For others who may end up here...

Make sure the image file is actually a part of the project (Resources\drawable) and that the build action is AndroidResource.




回答3:


When binding to the name of an image resource in android I found it must be:

  • in Resources/Drawable folder
  • set Build Action: AndroidResource
  • set Copy to Output: Do not copy
  • doesn't allow hyphens in name
  • name is case sensitive



回答4:


For iOS

Step 1: Add your image into ios -> Resources folder (If not create it)

Step 2; Right click image -> Properties -> Build action -> set as "Content"




回答5:


I had this issue.I set MSBuild project build output verbosity as Diagnostic. Now I found the following in my Output window when I searched for OOM.

ImageRenderer: Error loading image: Java.Lang.OutOfMemoryError: Failed to allocate a 571513228 byte allocation with 2140744 free bytes and 92MB until OOM

Now tried

  • Create a png image which has less than 200KB size and less than 1400 X 1050 size (for testing purpose).

It worked fine.

Note: "MSBuild project build output verbosity" can be found under Tools -> Options -> Projects and Solutions -> Build and Run

General Checkpoints

  • Read Local Images
  • Make sure the file name has only lowercase alphabets.
  • Add that png file into Resources/drawable folder.

Create a content page as follows

<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
    <Label Text="Pre" />
    <Image Source="abstracttriangleg.png"  
           Aspect="AspectFill" VerticalOptions="End" HorizontalOptions="CenterAndExpand"/>
    <Label Text="Post" />
</StackLayout>
  • Clean solution.
  • Clean bin and Obj files.

Resources says:

Android supports bitmap files in three formats: .png (preferred), .jpg (acceptable), .gif (discouraged).

Compress PNG and JPEG files says:

You can reduce PNG file sizes without losing image quality using tools like pngcrush, pngquant, or zopflipng. All of these tools can reduce PNG file size while preserving the perceptive image quality.

The pngcrush tool is particularly effective.

To compress JPEG files, you can use tools like packJPG and guetzli.

References:

  1. Android : Maximum allowed width & height of bitmap

  2. Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

  3. Handling Bitmaps



来源:https://stackoverflow.com/questions/35755089/xamarin-forms-image-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!