Xamarin 中的图像

Image in Xamarin

Xamarin 中的以下代码未显示图像。它在图像文件夹中。

var image = new Image { Aspect = Aspect.AspectFit };
image.Source = "images/image1.png";
Content = image;

我该如何解决这个问题?

注意:我使用的是表单应用程序。

您必须将图像放在每个平台项目的正确文件夹中。

  • iOS - 使用构建操作将图像放入 Resources 文件夹:BundleResource
  • Android - 使用构建操作将图像放在 Resources/drawable 目录中:AndroidResource.
  • Windows Phone / Windows / UWP - 将图像放在应用程序的 生成操作的根目录:Content

您可以找到更多信息 here

编辑(PCL-项目中的图像)

To embed an image in a project, right-click to add new items and select the image/s you wish to add. By default the image will have Build Action: None; this needs to be set to Build Action: EmbeddedResource.

然后加载图片FromResource(..):

var embeddedImage = new Image { Aspect = Aspect.AspectFit };
embeddedImage.Source = ImageSource.FromResource("test.jpg");

从 PCL-项目加载图像 -> 查看 this page