I've built a user control for my Xamarin Forms projects, starting with a Xamarin.Forms Class Library. The user control contains an image file that I've added to the Class Library project in an "Assets" directory. Within the user control code's XAML I'm simply referencing the image as...
<Image Source="Assets/ImageFile.png"/>
I'm thinking there's no need for platform-specific code here since the image file is local to the Class Library project and compiled into it.
When I reference the DLL in a Xamarin Forms project, everything works as expected...EXCEPT, there's no image. It's as if the Class Library can't see it.
I've played around with the path, but the result is always the same: no image.
However, if I drop the image file into the Xamarin Forms project (i.e., into the Assets directory in UWP), the image appears just fine -- even though I'm still referencing the Class Library through the DLL.
What am I missing? Surely I can embed the image within the DLL, yes?
To make sure the image is included within your DLL:
You need to set the Build Action: EmbeddedResource.
To embed an image in a project, right-click to add new items and select the image/s you wish to add. By default the image will have Build Action: None; this needs to be set to Build Action: EmbeddedResource.
If you need to know where to set this:
The Build Action can be viewed and changed in the Properties window for a file.
You can read more here.
Related
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:
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.
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.
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.
I'd like to share some assets like icons between multiple WinRT projects.
With WPF this was a no-brainer (well almost):
create a library project for the assets
mark the assets as resources to embed them into the generated assembly
reference the assets project from the other projects
reference the icons from the XAML code using the somewhat strange "pack" URI format.
What's the best way of sharing them with Windows Runtime?
Is there such a resource embedding and sharing capability, or any other solution?
If no I guess I could add them to every project with "Copy as link" but I hope there is a clean way.
EDIT: I've started to do it naively like I would in a WPF project:
I've created a new library project "Assets" and added the image inside as "Content"
I've referenced this project from my main project
But I can't reference the image with the new URI format:
<Image Source="ms-resource://Assets/Files/Mushroom.png"></Image>
So finally I got the correct result.
Here is the full process:
create your library project, add your image and set its build action as "Content"
reference the library from your main project
To reference the image itself you must:
use the "ms-appx" schema, not "ms-resource" as you might find on Google
specify an absolute path with /// not //
<Image Source="ms-appx:///Assets/Mushroom.png">
And above all don't trust the Visual Studio designer:
when you get it right it may not display the image
when you get it wrong it may display it (from a previous success) but at runtime you'll get nothing!
Hope this helps...
It is very simple. Right click on the folder where you want the images (e.g., an Assets folder in the new Project) and the select "Add/Existing Item". Then select the images that you need and be sure to change the "Add" button, to "Add as Link". Done.
What's the way to add custom images/icons in WinForms application.
I added a folder (right-click project and add new folder) named it then as my images folder and I would like to use this folder as my main images folder.
I don't see any option to use my 'images' folder in Visual Studio after dropping the Picture Box control.
Add the image as an embedded resource. You can the set the PictureBox's Image property to the resource via the property editor or at runtime by accessing the Properties.Resources object. The images will be compiled directly into your executable, you just need to add them to your project.
I am using visual studio 2005, (.net version is 2.0+) to create a windows application. The functionality of the project is matching with the ideal design, there is just one problem in publishing the project.
I use MouseHover method to change the picture(image) used in intention to make attractive UI, when I hover the mouse pointer over the picture .. some other pic is loaded in-place of it .. and in mouseleave method the same picture is retained back.
Now the problem is while debugging this functionality works properly,
But when published, and used, the window won't load the image (as the installed folder doesn't contain these images) .. How to bind the supporting files like images, text files and any other files like xml with EXE??
I mean is there any ideal way to publish the project??
In your project, ensure that the images are set to Copy Always or Copy if Newer on the Copy To Output Folder property (F4).
That should ensure that when doing an XCopy deploy the images will be in the right folder (you will proabably need to change the logic for finding the image paths, so the application finds them in the right directory).
If you are using deployment project that will generate .MSI for your application, just right click on the project, click add files and then pick the images from your project directory.
Those files will be (by default) be deployed into the application target directory, and your end-user app will be able to access it.
You can package them as Embedded Resources.
To embed them, make sure that when the resources are selected in the Solution Explorer that the 'Build Action' in the Properties window is set to 'Embedded Resource'.
Here is an example with sample project on how to embed images : How to use embedded resources in .Net
I have a silverlight class library (not a top-level application) with several user controls that are shared by different silverlight applications. I have gone through and pulled out several styles and brushes for the controls and put them in a separate XAML file as a resource dictionary which I bring into each control as a merged resource dictionary. Right now this works great in my top-level applications, as long as I keep that resource dictionary as a "Resource" build action and reference it in that way from the controls.
What I would really like to do is have this resource dictionary XAML file as a "Content" build action which gets copied into the top-level XAP, so that the XAML can be swapped out in the XAP file without needing to rebuild the project. Whenever I try to set this up, the XAML file will get copied into the output bin for the class library project, but it will never get copied to my top-level silverlight application project output directory or into the final XAP file.
What is the best way to accomplish this? The XAML resource dictionary is essentially a dependency of the class library, and the class library (of user controls) is a dependency of the top-level silverlight application.
Add the ControlResources.xaml (the ResourceDictionary you want in your XAP) to the top-level XAP project as a link via Project->Add Existing Item->Add as Link (a drop down item on the Add button in the dialog), and set it to build as Content, also.
Will merged dictionaries meet your needs?
http://www.silverlight.net/learn/videos/silverlight-videos/hdi-sl3-merged-resources/
http://thoughtjelly.blogspot.com/2009/10/cross-project-mergeddictionaries-in.html
http://www.liquidjelly.co.uk/supersearch/?q=silverlight%20merged%20resource%20dictionaries&lang=en-GB
Best,
Mark