SuavePirate / Xamarin.Forms.Controls.FloatingActionButton

A custom view to create a FloatingActionButton for both Android and iOS as part of Material Design
MIT License
120 stars 38 forks source link

How to set resource embedded for image? #32

Open robertorp opened 5 years ago

robertorp commented 5 years ago

How to set resource embedded for image?

example:

<views:FloatingActionButton HorizontalOptions="CenterAndExpand" 
                                        Command="{Binding NovoClienteCommand}"
                                           VerticalOptions="CenterAndExpand" 
                                           Image="{resources:ImageResource plus.png}" ButtonColor="#03A9F4"
                                           RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=-90}"  
                                           RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1, Constant=-90}" />
[ContentProperty(nameof(Source))]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

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

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

            return imageSource;
        }

        private string ImageNameFromResource(string u)
        {
            var assembly = typeof(App).GetTypeInfo().Assembly;
            foreach (var res in assembly.GetManifestResourceNames())
            {
                if (res.Contains(u))
                {
                    return res;
                }
            }
            return null;
        }
    }