简体   繁体   English

Xamarin.forms 嵌入图像未在 iOS 上显示

[英]Xamarin.forms embedded image not shown on iOS

I am building an app with xamarin.forms.我正在使用 xamarin.forms 构建一个应用程序。 Because the image is to be displayed on both devices, I choose to use an embedded image and followed this tutorial.由于图像要在两种设备上显示,我选择使用嵌入图像并遵循教程。

The build action of my image is configured to EmbeddedRessource我的图像的构建动作被配置为 EmbeddedRessource

在此处输入图片说明

I created the custom class :我创建了自定义类:

using System;
using System.Reflection;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

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

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

            return ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
        }
    }
}

And I load my image via the XAML:我通过 XAML 加载我的图像:

<Image x:Name="ChordStatusImage" AbsoluteLayout.LayoutBounds="0.66,0.33" AbsoluteLayout.LayoutFlags="XProportional,YProportional" />

The only slightly different thing is in the xmlns step: I cannot set the xmlns property as specified in the tutorial because I am already setting it to another value to use a custom renderer to display AdMob ads in my app (I followed this tutorial).唯一稍有不同的是 xmlns 步骤:我无法设置教程中指定的 xmlns 属性,因为我已经将其设置为另一个值,以使用自定义呈现器在我的应用程序中显示 AdMob 广告(我遵循了教程)。 But it doesn't seem to be a problem on Android.但在Android上似乎不是问题。

I set the resource for the image programmatically:我以编程方式设置图像的资源:

ChordStatusImage.Source = ImageSource.FromResource("PianoTraining.assets.images.wrongChord.png");

The problem is that my image is never shown on iOS and can't figure out why.问题是我的图像从未显示在 iOS 上,无法弄清楚原因。

[edit] Precision about how I set the source [编辑] 关于我如何设置源的精确度

The page where the image is loaded is called PianoTrainingPage so I have a class called PianoTrainingPage .加载图像的页面称为PianoTrainingPage所以我有一个名为PianoTrainingPage的类。 The image displayed should change depending on whether the chord is detected or not, and a function is called to load the right image.显示的图像应根据是否检测到和弦而改变,并调用一个函数来加载正确的图像。 But because the images weren't displayed, I only set one image programmatically in that function for testing purposes.但是因为没有显示图像,我只在该函数中以编程方式设置了一个图像以进行测试。

I tested several calls to ImageRessource.FromRessource and none of theme solved my problem:我测试了几次对ImageRessource.FromRessource调用,但没有一个主题解决了我的问题:

  • ChordStatusImage.Source = ImageSource.FromResource("PianoTraining.assets.images.wrongChord.png",typeof(ImageResourceExtension).GetTypeInfo().Assembly); (The one from the tutorial) --> The image is displayed on Andrid but not on ios (教程中的那个)-->图像在Andrid上显示但在ios上不显示
  • ChordStatusImage.Source = ImageSource.FromResource("PianoTraining.assets.images.wrongChord.png",typeof(PianoTrainingPage).GetTypeInfo().Assembly); (because I thought that maybe I was giving the wrong class to typeof) --> The image is displayed on Android but not on ios (因为我认为可能是我给 typeof 错误的类)--> 图像显示在 Android 上,但不在 ios 上
  • ChordStatusImage.Source = ImageSource.FromResource("PianoTraining.assets.images.wrongChord.png"); (using default value for the second parameter) --> The image is displayed on Android but not on ios (第二个参数使用默认值) --> 图像在Android上显示但在ios上不显示

In my code, I used the third version because it is the last I tested在我的代码中,我使用了第三个版本,因为它是我测试的最后一个版本

As Jason and ToolmakerSteve mentioned in the comment, there are two ways to solve the problem .正如 Jason 和 ToolmakerSteve 在评论中提到的,有两种方法可以解决这个问题。

Xaml xml

Add another name for your namespace , it is allowed to add same namespace with different names.为您的命名空间添加另一个name ,允许添加具有不同名称的相同命名空间。

xmlns:a="clr-namespace:PianoTraining"
xmlns:b="clr-namespace:PianoTraining"
xmlns:c="clr-namespace:PianoTraining"

And then set the reference to the class ImageResourceExtension ,like然后设置对类ImageResourceExtension的引用,比如

<Image Source="{a:ImageResource PianoTraining.assets.images.wrongChord.png}" />

Code behind背后的代码

Just provide any class which is included in the current assembly ,like ImageResourceExtension .只需提供包含在当前程序集中的任何类,如ImageResourceExtension

ChordStatusImage.Source = ImageSource.FromResource("PianoTraining.assets.images.wrongChord.png",typeof(ImageResourceExtension ).GetTypeInfo().Assembly);

It turns out I was doing things well from the beginning, with the typeof value, but for some reason, the data from the mic of my mac were not provided to the ios simulator.事实证明,我从一开始就做得很好,使用typeof值,但由于某种原因,我的 mac 麦克风的数据没有提供给 ios 模拟器。 I recompiled my binding library and everything is okay now.我重新编译了我的绑定库,现在一切正常。

Thank you for the help感谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM