Skip to content

How to set resource embedded for image? #32

Description

@robertorp

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;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions