Skip to content

Instantly share code, notes, and snippets.

@Adam--
Last active October 3, 2019 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adam--/6849f41906b5d847d9a18c1cffbcb08e to your computer and use it in GitHub Desktop.
Save Adam--/6849f41906b5d847d9a18c1cffbcb08e to your computer and use it in GitHub Desktop.
Load image resource from netstandard library in Xamarin Forms (from https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ThemingDemo)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="4.2.0.709249" />
</ItemGroup>
<ItemGroup>
<Folder Include="Images\" />
</ItemGroup>
<ItemGroup>
<None Remove="Images\ExampleImage.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Images\ExampleImage.png" />
</ItemGroup>
</Project>
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Example
{
[ContentProperty("Source")]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
return null;
return ImageSource.FromResource(Source);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Example"
...
...
<Image Source="{local:ImageResource ThemingDemo.Images.ExampleImage}" Aspect="AspectFill" />
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment