testing
testing

Reputation: 20279

Use Icon property with embedded images in Xamarin.Forms

I'm working on a shared project and I implemented a Master Detail Page. Now I added an image to the shared project (aka embedded image). The build action for the image is Embedded resource and I'm following the advice taking the namespace (HelloForms) and the subfolder (Ressources) into account. The result should be the following:

http://www.xforms-kickstarter.com/SlideOutMenu/screenshot-menu.png

As you can see the leftBarButtonItem is set through the Icon property. I tried to set the Icon property like the following:

Icon = Device.OS == TargetPlatform.iOS ? "HelloForms.Ressources.menu.png" : null;

and

Icon = new FileImageSource { File = "HelloForms.Ressources.menu.png" };

Currently I get the title of the Master page shown instead of the Icon. What do I have to change to get this working? I'm interested in the solution of embedded images and less in local images.

Upvotes: 3

Views: 5352

Answers (2)

Gusman
Gusman

Reputation: 15151

Your code is wrong, you want to use ImageSource.FromResource, that's explicitly created for that:

Icon = ImageSource.FromResource("HelloForms.Ressources.menu.png");

EDIT:

Icon is a FileStream so ImageSource cannot be used. Also, Xamarin did it for a good reason, that file will be used to represent the app on the desktop on Android so it must be accessible by the system and an embedded resource cannot be as it's inside a resource file.

"Embedded resource" is causing confusion here, on iOS projects Xamarin changed the default "Content" action to "Embedded resource", which leads to think it's being embedded in the .net assembly, but no, it's copied to the project.

Just add the file to the iOS/Android project as resource and use the file name directly.

Upvotes: 3

testing
testing

Reputation: 20279

menu.png was added to the shared project which is wrong. Adding it to the Resources folder of the iOS project (Build Action = BundleResource) seems to do the trick. Then you only need this code

Icon = Device.OS == TargetPlatform.iOS ? "menu.png" : null;

to make it work. Seems that I misunderstood Embedded Images.

Upvotes: -1

Related Questions