How to Add embedded image in Shared project not in PCL?

Renuka3Renuka3 Member ✭✭

Hello,

I tried to add embedded image in my shared project.

I created Shared project for Android & IOS.

Added images in shared project.

For rendering, in XAML file :

in same namespace **AllControl **I implemented interface IMarkupExtension.

[Preserve(AllMembers = true)]
[ContentProperty(nameof(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, typeof(ImageResourceExtension).GetTypeInfo().Assembly
        Console.WriteLine(typeof(ImageResourceExtension).GetTypeInfo().Assembly);
        var imageSource = ImageSource.FromFile(Source);
        return imageSource;
    }
}

Set image property Build Action as Embedded resource

Same code is working fine for PCL.

Please give suggestion for Shared project.

Thank you

Best Answers

  • ESBOXESBOX US ✭✭
    Accepted Answer

    Hi.
    I do not add images in shared project. I add them in platform project (Android, iOS and UWP).

Answers

  • ESBOXESBOX USMember ✭✭
    Accepted Answer

    Hi.
    I do not add images in shared project. I add them in platform project (Android, iOS and UWP).

  • Renuka3Renuka3 Member ✭✭

    @Renuka3 said:
    Hello,

    I tried to add embedded image in my shared project.

    I created Shared project for Android & IOS.

    Added images in shared project.

    For rendering, in XAML file :

    in same namespace **AllControl **I implemented interface IMarkupExtension.

    [Preserve(AllMembers = true)]
    [ContentProperty(nameof(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, typeof(ImageResourceExtension).GetTypeInfo().Assembly
            Console.WriteLine(typeof(ImageResourceExtension).GetTypeInfo().Assembly);
            var imageSource = ImageSource.FromFile(Source);
            return imageSource;
        }
    }
    

    Set image property Build Action as Embedded resource

    Same code is working fine for PCL.

    Please give suggestion for Shared project.

    Thank you

    @ESBOX said:
    Hi.
    I do not add images in shared project. I add them in platform project (Android, iOS and UWP).

    Did u try to add images from Assets folder in platform project?

  • Renuka3Renuka3 Member ✭✭

    Did u try to add images from Assets folder in platform project?

    I do not want to load images from Resources\drawable folder.

    Please suggest if we can load images from Assest or any custom folder from shared project or platform project.

    (Above code is working for PCL. Please suggest for shared xamarin project.)

  • ColeXColeX Member, Xamarin Team Xamurai
    edited March 22

    Shared project is not a real project , the image added into shared project will be merged into the Platform specific project .

    So you just need to change the image path from projectName.image.icon to projectName.platform.image.icon in xaml.

    Refer https://stackoverflow.com/a/53327736/8187800.

    Check my testing sample .

  • Renuka3Renuka3 Member ✭✭

    I used static images so that I don't need to reload image every time..
    Thank you .

Sign In or Register to comment.