Replaced by:
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️
This repository holds the samples for the XamSvg Xamarin control (Vapolia.Xamarin.Svg.Forms and Vapolia.Xamarin.Svg for UWP, Android and iOS). More infos
Xamarin.Forms (Android, iOS, UWP) | Xamarin Native (Android, iOS, UWP) |
---|---|
[![NuGet][forms-img]][forms-link] | [![NuGet][xamsvg-img]][xamsvg-link] |
[![][xamsvglivedemo-img]][xamsvglivedemo-link] | [![][xamsvglivedemo-img]][xamsvglivedemo-link] |
[![][formsdemo-img]][formsdemo-link] | [![][formsdemo-img]][demo-link] |
Xamarin Forms controls: SvgImage
and SvgImageSource
Xamarin Android controls: SvgImageView
and SvgPictureDrawable
Xamarin iOS controls: UISvgImageView
Native UWP controls: Svg
ImageSource="{vapolia:Svg refresh.svg,Height=50}"
Upcoming demo:
Note: there is no startup code needed! Anywhere!
Create a folder "images" at the root of your netstandard project (the project containing the App.cs
file) and put your svg files there.
Make sure they have the .svg
extension. And set their build action type to embedded resource
(important!) in the file property window.
SvgImage
control or the SvgImageSource
control<?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:svg="clr-namespace:XamSvg.XamForms;assembly=XamSvg.XamForms"
x:Class="XamSvg.Demo.MainPage"
IconImageSource="{svg:Svg myicon.svg,Height=60}">
<ContentView>
<StackLayout Orientation="Vertical" VerticalOptions="Start">
<svg:SvgImage Source="logo.svg" HorizontalOptions="Start" HeighRequest="32" />
<svg:SvgImage Source="logo.svg" HorizontalOptions="Start" HeighRequest="32"
ColorMapping="{Binding ColorMapping}"
ColorMappingSelected="ffffff=>00ff00,000000=>0000FF"
/>
<svg:SvgImage WidthRequest="100" Source="https://upload.wikimedia.org/wikipedia/commons/1/15/Svg.svg" />
<svg:SvgImage WidthRequest="100" Source="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlb..." />
</StackLayout>
</ContentView>
</ContentPage>
Remarks:
xmlns:svg
attribute is required on the ContentPage
. If you use Resharper, it will be added automagically.trick: You can also use <SvgImageSource Svg="...." Height="50" />
as the ImageSource for tab icons, button icons, navigation bar icons, ... But the Widht and/or Height is mandatory, as the Xamarin Forms controls infrastructure has a limitation: it has no way to dynamically give the target height to ImageSource objects.
trick^2: use the compact syntax: ImageSource="{controls:Svg refresh.svg,Height=50}"
XamSvg supports remapping color based on a change in the control's state, like selected or disabled.
To specify a color mapping, set the ColorMapping, ColorMappingSelected or ColorMappingDisabled properties to a string. This string contains a list of mapping separated by "," or ";". A mapping has two parts, separated by "=" or "=>". The left part is the color that should be replaced by the right part.
A color is specified using standard html coding: AARRGGBB
, RRGGBB
, or RGB
. A is the transparency (alpha channel).
For example ffffff=>00ff00,000000=>0000FF
means replace ffffff
(white) by 00ff00
(green) and replace 000000
(black) by 0000FF
(red).
Simple svg image
<svg:SvgImage Source="logo.svg" HeightRequest="70" HorizontalOptions="Center" VerticalOptions="Center" />
Svg image on a button
<Button Text="Add Contact" ContentLayout="Right,20" ImageSource="{svg:Svg tabHome.svg,Height=60,ColorMapping='000000=>FF0000'}" />
Svg image for the icon of a TabbedPage tab
<NavigationPage Title="Home" IconImageSource="{svg:Svg tabHome.svg,Height=60}">
<x:Arguments>
<views:HomePage />
</x:Arguments>
</NavigationPage>
Svg image with bindable color mapping
<svg:SvgImage Source="logo.svg" HeightRequest="60">
<svg:ColorMapping OldColor="#000" NewColor="{Binding CurrentColor}" />
<svg:SvgImage.ColorMappingSelected>
<svg:ColorMapping OldColor="#000" NewColor="{Binding CurrentColor}" />
</svg:SvgImage.ColorMappingSelected>
</svg:SvgImage>
If nothing appears, make sure your svg is displayed correctly by the windows explorer (after you installed this extension).
Common errors include
The assembly in which the svg resources are must have an assembly Name
equal to its Default namespace
, otherwise the svg files will not be found. If you are unable to do so, you can still display an svg but you will have to specify the full name of the resource like this:
<xamForms:SvgImage
Source="YourDefaultNamespace.Images.getFDR_01_Ready.svg"
x:Name="SvgIcon" HorizontalOptions="Fill" VerticalOptions="Start" Margin="8,0,8,0" BackgroundColor="Yellow"
/>
You can discover the full name of an embedded resource by opening your assembly (.dll in your bin folder) in the free tool Telerik JustDecompile
.
Another common error is setting the ImageSource
of a Button
in a style or in a trigger. Instead you must set the Image
property.
This is because of this issue.
<Style x:Key="FAB" TargetType="Button">
<!-- correct -->
<Setter Property="Image" Value="{xamForms:Svg create.svg,Height=50,ColorMapping='000=>634109'}"/>
<!-- incorrect
<Setter Property="ImageSource" Value="{xamForms:Svg create.svg,Height=50,ColorMapping='000=>634109'}"/>
-->
</Style>
Icons on TabbedPage
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:XamSvg.Demo;assembly=XamSvg.Demo"
xmlns:svg="clr-namespace:XamSvg.XamForms;assembly=XamSvg.XamForms"
x:Class="XamSvg.Demo.Pages.TabContainer"
Title="Vapolia.fr XamSvg Demo"
>
<views:HomePage Title="Home" IconImageSource="{svg:Svg tabHome.svg,Height=60}" >
</TabbedPage>
Mvvmcross
The library is fully compatible with mvvmcross bindings for all properties:
image source, color mappings, and all others.
Android native: make the svg image height the same height of a Button
Android native: set back button toolbar icon
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
toolbar.NavigationIcon = SvgFactory.GetDrawable("res:images.webbrowser.backward", "000000=FFFFFF");
Android: link all
iOS storyboard: usage in an xcode storyboard
Default namespace:
xmlns:svg="clr-namespace:XamSvg.XamForms;assembly=XamSvg.XamForms"
SvgImage
displays a image in up to 2 states: normal, selected
<svg:SvgImage Source="union.svg" HeightRequest="70" HorizontalOptions="Center" VerticalOptions="Center" />
Property | Type | Notes |
---|---|---|
Source | string or SvgSource |
svg to display. |
ColorMapping | string or ObservableCollection<ColorMapping> |
see color mapping reference. Default to none. |
ColorMappingSelected | string or ObservableCollection<ColorMapping> |
color mapping when IsSelected="True". Default to none. |
IsSelected | bool | used to switch color mapping |
IsSelectionEnabled | bool | True by default: the value of IsSelected is also inherited from the parent container |
Command | ICommand | if set, execute this command on tap |
CommandParameter | object | parameter to send when calling Command.Execute |
Width | double | Optional. You can also specify the width only and height will be computed from the aspect ratio |
Height | double | Optional |
FillMode | FillMode | Fit, Fill, Crop. Useful only if both width and height are forced. Default to Fit to maintain the aspect ratio. |
IsLoadAsync | bool | set to False to disable async image loading, making the image appear immediatly. Default to True. |
IsHighlightEnabled | bool | if set, ColorMappingSelected is used while the image is pressed (until the finger is released) |
ViewportTransform | IMatrix | transform the svg using any matrix before displaying it |
SvgImageSource
inherits from ImageSource
, use it on any ImageSource
property. For example Page.IconImageSource
.
It can also be transformed into a FileImageSource
by calling CreateFileImageSource()
.
SvgImageSource can be used in Button.ImageSource, ToolbarItem.IconImageSource, ...
<svg:SvgImageSource Source="tabHome.svg" Height="50" />
Property | Type | Notes |
---|---|---|
Source | string or SvgSource |
svg to display. |
Width | double | Optional. You can also specify the width only and height will be computed from the aspect ratio. |
Height | double | Optional. |
ColorMapping | string or ObservableCollection<ColorMapping> |
see color mapping reference. Default to none. |
SvgFillMode | FillMode | Fit, Fill, Crop. Useful only if both width and height are forced. Default to Fit to maintain the aspect ratio. |
PreventTintOnIos | bool | Default to false. Prevents tinting on iOS, thus always displaying the original image. |
All properties are bindable, but Xamarin Forms does not support changing them after the control using this SvgImageSource is rendered. Alternatively, you can bind the ImageSource property on the target control, and define SvgImageSource in styles. Example:
<svg:SvgImageSource x:Key="NormalIcon" Source="icon_normal.svg" Height="80" />
<svg:SvgImageSource x:Key="SelectedIcon" Source="icon_selected.svg" Height="80" ColorMapping="FFF=>000" />
<Style x:Key="NormalIconStyle" TargetType="ImageButton">
<Setter Property="Source" Value="{StaticResource NormalIcon}"/>
<Setter Property="BackgroundColor" Value="Transparent"/>
</Style>
<Style x:Key="SelectedIconStyle" TargetType="ImageButton">
<Setter Property="Source" Value="{StaticResource SelectedIcon}"/>
<Setter Property="BackgroundColor" Value="Transparent"/>
</Style>
And usage:
<ImageButton Style="{Binding StyleKeyToUse}" />
public string StyleKeyToUse {get;set;} = "NormalIconStyle";
//Don't forget to call OnPropertyChanged(nameof(StyleKeyToUse)) after each change.
The svg files that don't specify the full path of an assembly are searched in the embedded resources of all loaded assemblies automatically. If you have late loading assemblies that are not detected, or if you prefer to manually specify the assemblies, add this line:
public class App : Application
{
public App()
{
XamSvg.Shared.Config.ResourceAssembly = typeof(App).Assembly;
...
}
...
Trick: you can add more than one assembly by using the ResourceAssemblies
property instead.
On Windows install the microsoft powertoys to preview the svg files in windows explorer.
Commercial support available.