Images in a WPF Custom Control Library - wpf

I need to put an image in the default view of a custom control. However, whenever I try to test the control it can't locate the image. I have tried to compile it as an embedded resource and just a plain resource in VS. Neither of these have worked. So is there a correct way to do this?

That's probably because you specified the image path as a relative path. You should use the Pack URI Scheme to specify that the resource is in the current assembly. For instance :
<Image Source="pack://application:,,,/Images/MyImage.png"/>

I have an open-source library that allows you to include country flags in your WPF application via a value converter. The flags images are stored as resources within the assembly.
It's available on NuGet:
Install-Package FamFamFam.Flags.Wpf
The source is up on GitHub:
https://github.com/drewnoakes/famfamfam-flags-wpf
You can take a look to see how the images are embedded and the Pack URI scheme is used.

Related

VS2015 - DLL Manifest visibility in Description pane of Object Browser

I am creating a few WPF Custom Controls in VS2015 (C#) which I would like to distribute to a Shared library for other Development Team members. In order for these folks to reference the namespaces in these DLLs the preferred manner would be to use a xmlns:prefix with a URI rather than the clr-namespace syntax. I have set (in the AssemblyInfo.cs of the Custom Control) the
[assembly: XmlnsDefinition("http://www.DPACMediaSystem.com/CustomControls/", "DPACCustomControl0012.Controls")]
The developers actually use the URI and everything works ok..BUT..
I would like them to be able to see this URI in the Object Browser in the Description pane of the particular DLL.
Simple question, eventhough I can see that it exists in the manifest of the DLL through ildasm.exe, is there some setting on building the DLL that I must set to make this visible in the Object Browser ?
Any help appreciated..
Cheers,
Dezzz.

image file resources to image source in silverlight app

I want a Listbox that lists all the images I have as resources in my app, then on selection changed the Image Source in my app is changed.
Need to:
Enumerate all the image files,
Load them into an imagesource
In WPF, I can just System.IO to grab the files from a directory, but I can't do this in silverlight.
how do I do this?
Don't think you can do that for files set as Resource or Content. It is possible for Embedded Resource (although Embedded Resource is not recommended for Silverlight. Don't think that an Image control can access them directly). See related discussion It suggests creating such a list during build time using T4

Best practice for using resources in a WPF project

I know and use two methods to store and access resources on application scope:
Properties\Resources.resx
create a folder and place for example images there, setting their build mode to Resource
What is the difference between the two methods in terms of performance and complexity, when should each be used and how are the resources best consumed in WPF and VB or C# code in each way?
Thanks in advance,
Julian
The "natural" way of referencing resources like images in a WPF project is your second option. You can use a relative URI to point to the image and WPF will lazy load it. You can reference resources in other assemblies using pack URI syntax.
Using Resources.resx will code-generate properties that loads resources when referenced. Resources can be strings, images, icons or a byte arrays. Using {x:Static} in XAML allows you to reference the static properties generated by the code-generator but often you will need a converter to convert the resource type into a type usable by WPF.
There is some support for localization using Resources.resx and if you want to provide a multi-lingual application you could store the translated strings in Resources.resx. However, WPF localization as described by Microsoft is not based on Resources.resx.
For images, the second option is much easier. For strings, the first option is probably easier but instead you could stay in XAML and create a ResourceDictionary.

IlMerge Silverlight Class Library with Custom Controls

I am trying to merge all the assemblies of an class library in a single .dll file.
I can merge all the assemblies using the Ilmerge but is that when I use the merged dll in a Silverlight application I am having trouble when a template is apply to my control and binding problems if I use a control that inherits with UserControl.
is there any solution to solve this problem?
The problem is that when the initial dlls are built the Xaml in the project is added as a resource in the dll. The code generated to load this xaml will look something like this:-
System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightLibrary1;component/MyControl.xaml", System.UriKind.Relative));
Note how the name of the dll forms part of the Uri need to fetch the xaml. I doubt IlMerge is able to spot that and fix it up. Hence once merged the Uris cannot be found.
A resolution for this is probably uglier than multiple references or simply creating another project that links all the code files involved.

Use a embedded Image in a Button Template in Silverlight 4

I would like to build a Template for my (edit)Buttons in Silverlight 4.
Therefore I want to include the Images as a embedded resource.
So my question is:
How can I use the embedded ressource images in the template for my button?
The ControlTemplate (TargetType="Button") is located in one external Ressources.xml.
regards
Christoph
In Silverlight you should be using "Resource", never "Embedded Resource" as the build action for resources.
The MSDN Reference on Resource Files gives a very good overview of resources in Silverlight and the URIs you should use to reference them. It also goes over the default fallback mechanisms used when the referenced file is not immediately found.
In general, you would reference an image source by a path relative to the referencing XAML like this:
<Button>
<Image Source="path/to/myimage.png"/>
</Button>
If the embedded image resource is located in a different assembly from the referencing XAML, you can use the short assembly name and component keyword like this:
<Button>
<Image Source="/MyShortAssemblyName;component/path/to/myimage.png"/>
</Button>

Resources