Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Load image file using ImageResourceExtension from another assembly

$
0
0

I am trying to load an image file from project A using an ImageResourceExtension in project B from a project C.
In project C I am using the image file name as it is saved in project A.
Inside the ImageResourceExtension I am attempting to translate this file name into the full path of the image as it is inside project A and eventually return this as a FileImageSource.

Sadly this does not seem to be possible? Below there is some code to illustrate the issue.

Project A

<ToolbarItem Name="Log Out"
                 Icon="{Extensions:ImageResource LogOffLightCustom.png}"
                 Command="{Binding LogOutCommand}"
                 Order="Primary"
                 Priority="0"/>

This project also houses the image under <NameSpace>.Publics.Images.WinPhone.<Image name>

Project B

[ContentProperty ("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }
    `public object ProvideValue(IServiceProvider serviceProvider)
    {
        if(Source == null)
        {
            return null;
        }`

       IRootObjectProvider rootObjectProvider = (IRootObjectProvider)serviceProvider.GetService(typeof(IRootObjectProvider));
        var nameSpace = rootObjectProvider.RootObject.GetType().Namespace;
        string nameSpaceMinusViews = nameSpace.Substring(0, (nameSpace.Length - 6));
        string publicsNamespace = string.Concat(nameSpaceMinusViews, ".Publics");

       string imageLocation = "";

        if(Device.OS == TargetPlatform.Android)
        {
            imageLocation = string.Concat(publicsNamespace, ".Images.Droid.", Source);
        }
        else if (Device.OS == TargetPlatform.iOS)
        {
            imageLocation = string.Concat(publicsNamespace, ".Images.iOS.", Source);
        }
        else if (Device.OS == TargetPlatform.WinPhone)
        {
            imageLocation = string.Concat(publicsNamespace, ".Images.WinPhone.", Source);
        }

        return FileImageSource.FromFile(imageLocation);

    }
}`

Project C

References both project A and B
Loads the View from project A as MainPage
Sadly does not get pretty image


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>