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.
Related
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.
How can I import .ico into .exe in c# ? (I want to have only one file)
In resources file I have: icon.ico
When i click on that icon, in properties I have: "Embedded Resource"
What I shoud do(change)?
trayIcon.Icon = new Icon("c:\\users\\wulp\\documents\\visual studio 2013\\Projects\\WifiSwitch\\WifiSwitch\\Resources\\icon.ico");
and, how I have to use relative path ?
thanks
I landed here searching for a written solution regarding icon embedding in the output assembly/executable. But this accepted answer is not really explaining anything, unless you know how the answer relates to the code as a concept. For the code part, the link given by Jonas died in meantime as well.
Set as embedded resource
But another way is to set the icon file (which is preferably) in your solution as embedded resource by right click on the icon file in your solution. And set the field 'Build Action' to 'Embedded Resource'. Default it is set to 'Content', which makes the icon file appear as a separate file in the bin/output folder.
Then load the icon from the assembly/executable file at runtime by code. This is slightly different code than the idea Jonas probably meant to link (as he refers to resources in the project properties).
Usings for code below are
using System.Drawing;
using System.IO;
using System.Reflection;
//note: this = the current windows form I'm setting the icon for, in my case.
var assembly = Assembly.GetExecutingAssembly();//reflects the current executable
var resourceName = $"{this.GetType().Namespace}.{this.ProductName}.ico";
//note: this.ProductName reflects the executable's Product name in my solution (see current project, right click it, then click: properties - application - assembly information)
//reading the source / icon file, in this case from the assembly (as the icon is embedded in here), but the icon can be loaded from anywhere at this point.
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
this.Icon = new Icon(stream);
}
And to have a correct icon displayed on the shortcut or taskbar, make sure the icon file contains several sizes (variating from 48px by 48px to 256px by 256px). Else it shows a resized/upscaled icon.
Set icon on application property level
At last, set the icon for the project by clicking right on the project (in solution explorer) and then click: properties - application. Here at 'Resources' pick the .ico file in your solution. As it's embedded in the assembly this way, it won't need a copied version in the bin / output folder. But I assume the output assembly now includes two icons, one as embedded resource and one somewhere hidden in the assembly's bytes... as leaving out one of them breaks icon displaying functionality showing a default icon or not loading the embedded resource icon. The benefit of this answer is, as input, there is only one icon file, which is best for maintaining it.
Possible glitch on your machine is icon caching by Windows
Possibly, the machine's icon cache can be an issue. This mostly is occurring when developing the application and switching icons, displaying a former/previous icon. Search the internet for "reset icon cache windows", or this example here https://www.windowscentral.com/how-reset-icon-cache-database-windows-10.
You have to add your icon as a resource.
See: https://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
When you've added it as a resource you can point to it.
Assuming that icon.ico is in your project folder, edit your .csproj file:
<PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon>
<PropertyGroup>
<ItemGroup>
<Resource Include="icon.ico" />
</ItemGroup>
That's it.
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.
I don't know if I asked it right, but basically what happened is that I made a winform app which loads its image from the resource folder.
The problem is that when I build the project and get the exe and give it to a friend, he won't have that resource folder like I do, so he'll get an error saying missing file.
How can I somehow mix, or combine, or attach the image with my app?
You need to add it to the project by navigating to the Properties Window and going to the resource tab and adding the image from there.
Alternatively, from the PictureBox Control you can import resource from your computer.
if you don't need to update it in the future, compile your program with the image property build action set to embedded resource.
if you need to change it in the future compile with property build action set to content.
I set up a NotifyIcon to store my application in the system tray. For the Icon I was using
myNotifyIcon.Icon = new System.Drawing.Icon(#"c:\MyIcon.ico");
and this works. However I would like to store my icon in my project directory and refer to it where I deploy my app rather than have some external image. i tried just
myNotifyIcon.Icon = new System.Drawing.Icon("MyIcon.ico");
and this where i get the XamlParseException when I try to run the app. The image does exist in the root of my project. Does anyone know the solution to this? Should I be using a PNG instead? I tried researching Pack Uris but i was just getting more confused and i am not sure thats what i need here. Thanks!
It sounds like you are not copying the icon to the deployment directory on build. Make sure the .ico is sitting alongside your .exe rather than just in your project directory.