Home > Enterprise >  Xamarin.forms embedded image not shown on iOS
Xamarin.forms embedded image not shown on iOS

Time:10-27

I am building an app with xamarin.forms. Because the image is to be displayed on both devices, I choose to use an embedded image and followed enter image description here

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:

<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). But it doesn't seem to be a problem on 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.

[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. 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:

  • 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
  • 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
  • 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

In my code, I used the third version because it is the last I tested

CodePudding user response:

As Jason and ToolmakerSteve mentioned in the comment, there are two ways to solve the problem .

Xaml

Add another name for your namespace , it is allowed to add same namespace with different names.

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

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

Code behind

Just provide any class which is included in the current assembly ,like ImageResourceExtension .

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

CodePudding user response:

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. I recompiled my binding library and everything is okay now.

Thank you for the help

  • Related