【问题标题】:How to add an image to my Xamarin.Forms shared project's resources so that it can be used in both the Android and iOS projects?如何将图像添加到我的 Xamarin.Forms 共享项目的资源中,以便它可以在 Android 和 iOS 项目中使用?
【发布时间】:2020-11-26 21:26:11
【问题描述】:

我正在构建适用于 Android 和 iOS 的 Xamarin.Forms 应用,我想将图像添加到我的 Xamarin.Forms 共享项目中,以便它们同时显示在 Android 和 iOS 应用中。

这是我目前添加它们的方式:

这就是我在代码中引用它们并将它们动态添加到 Grid 元素的方式:

但是当我运行应用程序(使用 Android 模拟器调试器)时,我看不到网格中加载的图像。 我在输出窗口中收到以下错误:

[0:] 无法加载名为:{0}: Images\Blue-1.png

的图像

[0:] FileImageSourceHandler: 找不到图像或图像文件是 无效:文件:Images\Blue-1.png

我是否也需要将相同图像的副本添加到 ModelT.Android 和 ModelT.iOS 项目的资源中? (我认为主要共享项目的全部意义在于我不必这样做?)

【问题讨论】:

标签: c# image xaml xamarin xamarin.forms


【解决方案1】:

共享项目中的任何图像都需要设置为EmbeddedResource,并且需要将完整路径用于源。

var image = new Image
{
    Source = ImageSource.FromResource("ModelT.Images.****Blue-1.png")
};

更多信息可以在嵌入式图像部分的docs 中找到。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2018-02-15
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多