【问题标题】:Why Image not loading on android - xamarin为什么图像无法在 android 上加载 - xamarin
【发布时间】:2022-01-20 13:41:18
【问题描述】:

我把crypto.png图片放到Android项目Resources下的drawable文件夹中。

在我右键单击图像后 -> 构建操作 -> 嵌入式资源。

在主项目的MainPage.xaml 中,我尝试像这样加载图像:

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

但图片无法加载。

我在 c# 代码中尝试了这样的第二种方法:

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

这个方法又没用了..

【问题讨论】:

  • 你能举个例子吗,我会接受你的回答
  • 我尝试使用: HeadImage = new Image { Source = ImageSource.FromResource("crypto.png") };蚂蚁这对我来说不再有用了

标签: c# xamarin


【解决方案1】:

您需要将drawable 文件夹中的图片设置为Android Resource 而不是Embedded Resource,要使用它,您需要:

HeadImage.Source = "crypto.png";

另外,在设置图像之前,最好删除MinimumWidthRequest and MinimumHeightRequest。仅当您确定图像具有此最小尺寸时,这才是安全的。否则,您的图像将不会出现。

要了解有关 Xamarin 中图像的更多信息,请参阅here

要了解设置ImageSource 的方式之间的区别,请参阅here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多