Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an image from Embedded resource using XAML in Xamarin.Forms

When setting an image source in code and pointing towards an embedded image in a specific folder in the PCL project ('Images' in this instance) I would do this:

backgroundImg.Source = ImageSource.FromResource("Myapp.Images." + imageName + ".jpg");

Is there a way to do this in XAML?

like image 837
DarkW1nter Avatar asked Jan 08 '19 11:01

DarkW1nter


People also ask

How do you bind an image source in Xamarin forms?

In Xamarin. Forms SfAvatarView, you can set the key for ImageSource by using ResourceDictionary and bind image from ViewModel to ImageSource. An ImageSource instance, can be either File, Uri or Resource, which sets the image to display.

How do I add images to Xamarin?

Go to Start -> New Project-> select Cross-Platform (under Visual C# -> Blank App (Xamarin. Forms Portable)-> Give a suitable name to your app (XamFormImg) -> OK. Now, create project “XamFormImg_Droid”. Choose the target and minimum platform versions for your Universal Windows Project.

How to add images in Xamarin forms?

The first thing we have to do, is to add your needed images in to your Xamarin Forms project: . Add folder preferably named “Images” ( It’s not mandatory but it is good practice and makes your project more understandable and organized)

How to set image from XAML?

you can set image from xaml like <Image Source="imageName" Aspect="AspectFit" /> Make sure your image available in iOS Resources and in Android Resources -> drawable Thanks for contributing an answer to Stack Overflow!

What is local image in Xamarin?

Local Images. Image files can be added to each application project and referenced from Xamarin.Forms shared code. This method of distributing images is required when images are platform-specific, such as when using different resolutions on different platforms, or slightly different designs.

How do I add an image to an existing resource?

Android: Resources->Drawable (To Add image in the folder: right click on drawable -> Add -> Add existing item-> navigate to your image - > add) iOS: Resources (To Add image in the folder: right click on resources -> Add -> Add existing item-> navigate to your image - > add)


1 Answers

@Code Pope is correct in his answer, but you also need to add an "ImageResourceExtension" as noted in the comments by @Gerald Versluis.

To do that just create a new file (class) "ImageResourceExtension.cs" like this:

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

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

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

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

Then add xmlns:local="clr-namespace:ImageResourceExtension" to your xaml file like this:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:ImageResourceExtension"
         ...

Now you can access an embedded resource using xaml code like this:

<Image Source="{local:ImageResource MyProject.Resources.Images.Logo.png}" />
like image 122
Ola Ström Avatar answered Oct 24 '22 07:10

Ola Ström