【问题标题】:Embedded images not showing嵌入图像不显示
【发布时间】:2015-11-27 07:15:40
【问题描述】:

这是我在可移植项目中的页面

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:b="clr-namespace:Corcav.Behaviors;assembly=Corcav.Behaviors"
                 xmlns:local="clr-namespace:MyMobileApp;assembly=MyMobileApp"
                 x:Class="MyMobileApp.MainPage"
                 x:Name="MainPage">

      <Image Source="{local:ImageResource myimage.jpg}" />

这是我在同一个便携式项目中的 ImageResourceExtension

namespace MyMobileApp
{
    [ContentProperty("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
                return null;

            var imageSource = ImageSource.FromResource(Source);

            return imageSource;
        }
    }
}

我尝试将 myimage.jpg 添加为嵌入到我的项目的根目录和资源文件夹中,但没有显示图像。

在调试时,我看到返回的 imageSource 是 Xamarin.Forms.StreamImageSource 类型。如何检查是否真的找到了?

谁能发现这里的错误?

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    默认情况下,图像将具有构建操作:无;这需要设置为 Build Action: EmbeddedResource。 右键单击图像 > 属性 > 设置 [Build Action: EmbeddedResource] []1

    【讨论】:

    • 即使在使用应用名称后,我也无法加载图像。设置构建操作是我必须做的才能让它工作。谢谢奥萨马!
    • 我已经这样做了,但没有任何效果。如何将图像链接到我设置为嵌入资源的他的文件?
    【解决方案2】:

    正确的 XAML 是将应用名称添加到源。

    <Image Source="{local:ImageResource MyMobileApp.myimage.jpg}" />
    

    【讨论】:

      【解决方案3】:

      你能解释一下扩展的意图吗?假设您将图像放在正确的文件夹中(iOS 上的“Resources”,Android 上的“Resources/Drawable”),那么您在 XAML 中只需要:

      <Image Source="FeaturedAreaMockup.jpg" />
      

      这将在适当的文件夹中找到图像并显示它们 - 你为什么不这样做呢?扩展的意义何在?

      【讨论】:

      • 图像与 xaml 文件和视图模型一起位于共享项目中。我不想在每个项目上放置相同的图像。 developer.xamarin.com/guides/cross-platform/xamarin-forms/…
      • 啊,明白了。只要确保您这样做是为了特定用途。很多时候你会想要把它们放在每个平台项目中,这样你就可以在 iOS 上支持视网膜图像,并支持不同尺寸的图像(在 drawable-hdmi、drawable-xxhdmi 等中)以适应不同的屏幕密度(以及记忆能力)在 Android 上。
      【解决方案4】:

      要读取你需要使用的 assets/resources 文件夹:

      ImageSource.FromFile("myimage.jpg");'
      

      ImageSource.FromResource 使用包含为EmbeddedResource 的图像

      更多:https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/

      【讨论】:

      • 谢谢,但正如我所说,我嵌入了图像
      • 您还提到了 Resources 文件夹,该文件夹用于存储使用 ImageSource.FromFile 检索的图像。我虽然这会很有用。现在,我看到您使用单独的项目来获取资源。
      猜你喜欢
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2013-01-28
      • 2013-06-24
      • 2018-05-01
      相关资源
      最近更新 更多