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?
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.
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.
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)
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!
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.
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)
@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}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With