Set default xaml window background image in dll - wpf

I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D

You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.‌​jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.

Related

Why is my Image unable to locate the Resource?

I'm stumped. I am trying to display an image embedded in another assembly. I've tried several things, but nothing has worked so far. I'm convinced I'm missing something really simple.
Currently, the Xaml looks something like this:
<Image>
<Image.Source>
<BitmapImage UriSource="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
</Image.Source>
</Image>
I originally had Xaml that looked like this:
<Image Source="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
I switched it hoping it would make a difference. (It didn't.)
I have a reference to Name.Of.Assembly in the project I am trying to create an image. In the assembly, Name.Of.Assembly, there's a directory called Resources, which has a subfolder called Icons, which contains the image icon.png. The build action on icon.png is set to Embedded Resource.
When I try to run, I get a XamlParseException:
'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception'
Whose inner exception is:
'Cannot locate resource 'resources/icons/icon.png'
It's like it's not looking in the assembly for the resource. Why does it fail to load the image? What am I missing?
I found a solution to my problem:
The first issue was probably having my build action set to Embedded Resource instead of Resource. Once I changed this I ran a Build on the project containing the resource, but I noticed nothing different.
Per Sean Nelson's suggestion, I did a Clean, followed by a Rebuild on the project.
The image is now displaying.

Refer an image resource from Universal App shared project

I just started work with winRT, shared projects etc. and faced with some problems.
I want to put all of my resources (strings, images etc.) in one project (portable class library or shared project).
When I put an image in PCL everything works fine in xaml by referencing with ms-appx:
<ImageBrush ImageSource="ms-appx:///Resources/Images/baby.jpg" Stretch="Uniform"/>
But when I put a string resource in PCL, I have following weird xaml behavior with ResourceDictionary:
http://postimg.org/image/ysbkvv6d7/
Ok. Then I decided to put every resources in Shared project. For this time it works perfectly with strings but fails with images: I couldn't get the right URI string in ImageSource of ImageBrush.
So the questions are:
How can I add ResourceDictionary in PCL
What is the correct URI format to reference image from shared project.
Thanks in advance!
To access an image your shared project file, just use the direct file path. The
<Image Source="Assets/ImageName.png"></Image>
Edited by #JerryNixon
<Image Source="ms-appx:///Assets/ImageName.png"></Image>
To access an image in your PCL, use the longer syntax
<Image Source="ms-appx:///ProjectName/...path.../"/>

Embedded fonts works for designer but not for application

I am building a wpf application and I wanted to use Open Sans Regular font in my application.
I referred this link for embedding font. I added OpenSans Regular.ttf file to resources under project properties.
Then I referred them in my application as mentioned below:
<Window x:Class="FontEmbeddingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" FontFamily="Resources/#Open Sans">
<Window.Resources>
</Window.Resources>
<Grid>
<TextBlock Height="100" Text="This is test text." FontSize="14" FontFamily="Resources/#Open Sans"/>
</Grid>
</Window>
In designer of Visual Studio I can see font changed to open sans but when I run the application it is taking system default font (Arial in my system).
Do let me know if you need any other information.
From MSDN Page:
When you add fonts as resources to your application, make sure you are
setting the <Resource> element, and not the element
in your application's project file. The <EmbeddedResource> element for
the build action is not supported.
So, I guess, you will have to add this font file to your project as you would add any other file and set its BuildAction to Resource instead of adding the font to the Resources under Project Properties, which will make it as an EmbeddedResource.
EDIT
Read this excellent article on how to use custom fonts in a WPF application.
You could also, get this working by way #Sheridan mentioned - which is setting BuildAction to Content. However, the problem with this approach is you will have loose separate file hanging around along with your binary. Users can potentially change this file, which may cause problems with your app. It is best to use Resource as BuildAction as the font gets bundled into the assembly.
In my opinion, I believe that the accepted answer to this question may either be incorrect, or misleading. I am using Font files in exactly the same way and I have absolutely no need to set their BuildAction to Resource. My Font files have a BuldAction set to Content and that works just fine. I'm guessing that the accepted answer would only help users that have set their Font file to EmbeddedResource.
The comment that #sthotakura quoted from the linked MSDN page is merely talking about manually editing the project file, which the question author is not doing. Note that there is no mention of BuildAction property in the linked page apart from mentioning that the EmbeddedResource value is invalid in this case.
Please try this instead or as well:
Set the BuildAction of the Font file(s) to Content.
Reference the Font file like this (with the all important starting slash):
FontFamily="/Resources/#Open Sans"
I just tried removing the starting slash in my project and the Font defaulted to another Font, so I'm pretty sure that this will work... all the same, please let me know. I'm more than happy to remove this answer if I am mistaken.

Way to use WPF Image without attaching image at runtime

I know how to attach an image to a XAML <Image> via VB.net runtime code. How would I do this the normal old way. (Attach the image within the IDE without using any code)
Before WPF one would browse for the image (in the Visual Studio IDE) it would somehow place it in the project and it would work.
Now I do this same thing, I can see the image in my Visual Studio IDE but during runtime the image appears blank.
How would I use the VS IDE to correctly browse for the image so it will display at runtime? Perhaps im doing something wrong a bit of help or any guidance would be awesome
I suppose you have a folder in your project which contains your image files, in that case something similar to <Image Source="/WpfApplication1/component;ImagesFolder/ImageName.png" /> would do the trick.
If you're looking for a non-code way, when you click on tag, you can browse for imported files from Source Property from Properties panel

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