【问题标题】:Xamarin Forms - Image doesn't show up on LumiaXamarin Forms - Lumia 上不显示图像
【发布时间】:2018-04-22 03:40:04
【问题描述】:

我实现了一个以 UWP 平台为目标的 Xamarin 应用程序。 我在 PCL 的 Assets 中添加了 4 个图像,并在每个图像上设置了 Build Action = Embedded Resource。 UWP 项目中未添加任何图像。

如果我在笔记本电脑上运行该应用程序,一切正常,但当我在 Lumia 上运行相同的应用程序时,没有图像显示。我不明白为什么,因为该应用程序与目标平台的唯一区别是相同的,我的笔记本电脑为 x64/x86,我的 Lumia 为 ARM。

我也尝试为每张图片设置Copy to output Directory = Copy Always,但没有成功。

【问题讨论】:

    标签: image xamarin.forms uwp


    【解决方案1】:

    您是否尝试过其他平台(Droid、iOs?) 如文档中提到的:https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/,您需要:

    1) 使用自定义 MarkupExtension 从 EmbeddedResource 加载图像:

    [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;
     }
    }
    

    2) 使用此自定义标记扩展并提供图像的完全限定名称(命名空间 + 名称)

    <Image Source="{local:ImageResource MyProject.Assets.Images.beach.jpg}" />

    【讨论】:

    • 我现在无法在其他平台上测试该功能。是的,我这样做了,事实上,如果我在笔记本电脑上运行该应用程序,它就可以工作......
    • 只是为了确定。你的图片是真实的jpg吗? (不是用 jpg 重命名的 png)。我在这种情况下看到了奇怪的行为。也许您可以使用 png 图像进行测试?
    • 刚刚发布了解决方案。感谢您的帮助。
    【解决方案2】:

    刚刚找到解决方案,禁用“使用 .NET Native 工具链编译”。 https://stackoverflow.com/a/37352400/2297037

    您对可能的副作用有所了解吗?

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2016-06-15
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 2018-03-16
      • 2014-09-13
      相关资源
      最近更新 更多