【问题标题】:Image not displaying - xamarin emdeded resource Image data was invalid: Xamarin.Forms.StreamImageSource图像未显示 - xamarin 嵌入资源图像数据无效:Xamarin.Forms.StreamImageSource
【发布时间】:2018-06-17 08:23:54
【问题描述】:

我正在关注this 使用图像作为嵌入式资源的示例。

我已将图像设置为嵌入式资源,在 XAML 中添加了扩展名和对其的引用,使用了完整路径(使用点作为分隔符),但未显示图像。我在扩展中设置了一个断点,它被命中了。 代码和 XAML 都等价于链接的示例(只是图像的路径不同)。

我在输出窗口中收到以下错误: ImageLoaderSourceHandler:图像数据无效:Xamarin.Forms.StreamImageSource

有什么想法吗?

【问题讨论】:

    标签: image xamarin.forms embedded-resource


    【解决方案1】:

    我在学习相同的教程时遇到了同样的问题。幸运的是调试帮助解决了我的问题,也许它也可以帮助你。

    出现错误:“ImageLoaderSourceHandler:图像数据无效:Xamarin.Forms.StreamImageSource”我决定查看source code

    它仅在位图为空的情况下打印此错误。 可是等等。我知道我的位图不为空,所以问题必须介于两者之间 - 路径必须不正确。 使用他们在同一篇文章中发布的调试代码,在构造函数中调用:

    private void CheckResourcesFound()
    {
      // ...
      // NOTE: use for debugging, not in released app code!
      var assembly = typeof(<class-name-this-method-is-in>).GetTypeInfo().Assembly;
      foreach (var res in assembly.GetManifestResourceNames())
      {
        System.Diagnostics.Debug.WriteLine("found resource: " + res);
      }
    }
    

    我已经注意到@pnet 已经指出的——路径需要以这种方式构建

    <assembly>.<folders-separated-by-dot>.<resource-with-extension>
    

    我的图片在资源目录中。假设您的也是如此,那么您的 xaml 应该如下所示:

    <Image Source="{local:ImageResource 'projectname.Resources.imagename.png'}" .../>
    

    在调整和编译并最终运行后,我最终可以在我的应用中看到我想要的图片:)

    【讨论】:

      【解决方案2】:

      你必须像这样创建一个转换器:

      [ContentProperty ("Source")]
              public class ImageResourceExtension : IMarkupExtension
              {
               public string Source { get; set; }
      
               public object ProvideValue (IServiceProvider serviceProvider)
               {
                 if (Source == null)
                 {
                   return null;
                 }
                 // Do your translation lookup here, using whatever method you require
                 var imageSource = ImageSource.FromResource(Source);
      
                 return imageSource;
               }
              }
      

      这里是你的用户界面

      xmlns:local="clr-namespace:projectname;assembly=projectname"
      
      <ContentPage.Content>
              <StackLayout Spacing="10" Orientation="Vertical" x:Name="MainLayout">
                  <Image Source="{local:ImageResource projectname.imagename.png}"></Image>
              </StackLayout>
          </ContentPage.Content>
      

      希望对你有帮助。

      EDIT1

      和我一起工作。看截图

      我的xml:

      <?xml version="1.0" encoding="utf-8" ?>
      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                   xmlns:local="clr-namespace:Operacional"
                   x:Class="Operacional.MainPage">
      
          <ContentPage.Content>
      
              <StackLayout Spacing="10" Orientation="Vertical" x:Name="Layout">
      
                  <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                      <Image  Source="{local:ImageResource Operacional.Images.automotive.png}"></Image>
                      <local:Badge x:Name="OcupacaoNotifications" BoxColor="Red"
                                   WidthRequest="18" HeightRequest="18" 
                                   VerticalOptions="Center" HorizontalOptions="Center">
                          <x:Arguments>
                              <x:Double>30</x:Double>
                              <x:Double>10</x:Double>
                          </x:Arguments>
                      </local:Badge>
                  </StackLayout>
      
              </StackLayout>
      
          </ContentPage.Content>
      
      </ContentPage>
      

      我的班级

      [ContentProperty("Source")]
          public class ImageResourceExtension : IMarkupExtension
          {
              public string Source { get; set; }
              public object ProvideValue(IServiceProvider serviceProvider)
              {
                  if (Source == null)
                  {
                      return null;
                  }
                  // Do your translation lookup here, using whatever method you require
                  var imageSource = ImageSource.FromResource(Source);
      
                  return imageSource;
              }
          }
      

      通过这段代码,我可以看到上面的图像

      &lt;Image Source="{local:ImageResource Operacional.Images.automotive.png}"&gt;

      NameSpace.FolderName.ImageName.extension

      【讨论】:

      • 这看起来像我最初所做的。我复制了你的代码,它仍然不起作用。感谢您提供帮助。
      • {System.Reflection.TargetException:引发了“System.Reflection.TargetException”类型的异常。在 System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException()
      【解决方案3】:

      我一直在与这个例子作斗争,试图让它在我的代码中工作,但没有运气。直到我意识到该示例未使用共享项目,并且每个平台的应用程序的程序集名称不同,因此 XAML 无法找到它并包含 ImageResourceExtension。我在两个平台项目中重命名了输出程序集,并且它起作用了。

      我花了几个小时才找到那个金块。

      【讨论】:

        【解决方案4】:

        我不知道 4 年后你是否还需要这个问题的答案,但你可能忘记将图像 Build Action 设置为 Embedded resource

        在 VS 2022 中,您可以通过单击解决方案资源管理器中的图像来执行此操作。在
        Build Action 属性中选择Embedded resource

        【讨论】:

          猜你喜欢
          • 2019-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-15
          • 1970-01-01
          • 2013-06-24
          • 2020-06-10
          相关资源
          最近更新 更多