Created
August 26, 2015 14:20
-
-
Save softlion/1df832a1711fb38f00a2 to your computer and use it in GitHub Desktop.
Fix for XamSvg in a button on Xamarin Android Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:demo="clr-namespace:XamSvg.Demo;assembly=XamSvg.Demo" | |
x:Class="XamSvg.Demo.MainPage" | |
BackgroundColor="#E08080" | |
Title="XamSvg Demo"> | |
<demo:MyButton x:Name="TestButton" Text="TestText" /> | |
</ContentPage> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MainPage : ContentPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
//FileImageSource is sealed. We can currently not derive SvgImageSource from FileImageSource. | |
var svg = new SvgImageSource { Svg = "res:images.hand", HeightRequest = 20, ColorMapping = "ffffff=00ff00" }; | |
var image = svg.Image; | |
TestButton.Image = svg.Image; | |
} | |
} | |
@softlion thanks, I managed to make it load files from the filesystem, I'm positioning it here so maybe someone could improve on this to make it work with XamSvg.
public class ImageButtonRenderer : ButtonRenderer
{
Context context;
public ImageButtonRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
if (e.NewElement.Image != null && !string.IsNullOrEmpty(Element.Image.File))
{
var image = e.NewElement.Image;
e.NewElement.Image = null;
base.OnElementChanged(e);
e.NewElement.Image = image;
}
else
base.OnElementChanged(e);
int padding = (int)Math.Round(((ImageButton)Element).Padding);
Control.SetPadding(padding, padding, padding, padding);
Control.SetBackground(null);
Element.WidthRequest = Element.HeightRequest;
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == Button.ImageProperty.PropertyName)
UpdateBitmap();
else
base.OnElementPropertyChanged(sender, e);
}
private void UpdateBitmap()
{
if (Control == null)
return;
if (Element.Image != null && !string.IsNullOrEmpty(Element.Image.File))
{
var imagefile = Element.Image.File;
var scale = context.Resources.DisplayMetrics.ScaledDensity;
Drawable drawable = Drawable.CreateFromPath(imagefile);
drawable.SetBounds(0, 0, (int)(scale * drawable.IntrinsicWidth), (int)(scale * drawable.IntrinsicHeight));
Control.SetCompoundDrawables(drawable, null, null, null);
if (drawable != null)
drawable.Dispose();
}
else
Control.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work, I've tested with 2.5.0, GetDrawableFromEmbeddedResource function even doesn't exist in SvgFactory