WPF : Accessing themes and images from XAML - wpf

I have a file for applying styles to all controls ApplicationTheme.xaml and images in Images folder.
The build action is set to resource and copy to output directory always.
<Application.Resources>
<ResourceDictionary Source="Theme/ApplicationTheme.xaml"/>
</Application.Resources>
Now similarly i give the image source in the application theme file
ImageSource="Images\SGL-BG.jpg"
However i am getting error message
"'Failed to create a 'ImageSource' from the text 'Images\\SGL-BG.jpg'.'
What am i doing wrong here ? This is the approach i see mentioned on websites. Also if i give the full path it works. It fails to detect the theme file as well when i give Theme/ApplicationTheme.xaml. I am a WPF noob and i am not able to find solution to this seemingly simple issue.

Try this:
Source="/MyProject;component/Images/SGL-BG.jpg"

Related

XAML don´t know the Resource folder

I tried to set up a Icon in XAML from the Resources.resx but it cant find the resources.
Code:
....
xmlns:resx="clr-namespace:Admin_Overwatch.Properties"
Title="MainWindow" Height="400" Width="600" Icon="{x:Static
resx:Resources.TitelLogomRand1}">
Error:
"The name "Resources" doesen´t exist in the namespace...."
The courious thing is that in the autoformat it finds every icon in the Resources file. I have Rebuild it without any success and tried a new Resource folder also with no success.
Why doesn´t it find the Folder(s) ?
Edit:
I tried this tutorial also without any success, I got the same error, it can´t find the resources....
http://social.technet.microsoft.com/wiki/contents/articles/22420.binding-to-resources-resx-files-in-xaml.aspx
The Answer was to use the Assembly in addition to the normal clr:
xmlns:resx="clr-namespace:Admin_Overwatch.Properties;assembly=Admin-Overwatch"
xmlns:local="clr-namespace:Admin_Overwatch;assembly=Admin-Overwatch"
The Assembly name can be found by
Right Click on the Project name under Properties --> Application.
But the important part is that it is not possible to load the pictures from the resx in wpf that goes only in win forms.
see here:
How to use Resources.resx to link images
I had simmilar problem. Added a placeholder image to use as default ImageSource in XAML on startup. I created it trough Project+Right-Click->Properties>Resources>Add Resource>New Image etc.
Then got to Resource folder in the project, selected the image and down bellow in properties I just selected Build Action to Resource. And now it works!

XAML Resx localization not working as expected

I'm attempting to use a resx file to localize some strings I am using in a XAML file. I've looked around at other documentation on the web, and they all seem to recommend a two part process:
Add a clr-namespace to your window, like this:
xmlns:props="clr-namespace:PJConfiguration.Properties"
Use that namespace to localize your string like this:
Content="{x:Static props:Resources.SharedSettings}"
I've done this, and also made sure that my resource classes are public, but I still get the following error from the XAML in step 2:
Cannot find the type 'Resources'.
Does anyone know what else might be causing this problem? Thanks in advance.
In order to make the Resources visible to XAML, you have to make sure that the code generation mode for the resources is set to public. In VS, you find that setting in a ComboBox near the top of the Resources designer window.
For more information on using .Net resources in XAML, you might want to refer to these blog posts: http://wpfglue.wordpress.com/category/localization/
Check if your .resx file is the default Resources.resx file inside the Properties directory of the Application assembly. If that is, there is no reason XAML couldn't find the public class Resources from the correct namespace under local assembly.
Try to specify the assembly name in Step 2 as recommended in this answer.

Changing the startup file of a WPF project

I am sure this is a trivial problem, but it's one of which I cannot seem to solve.
I changed the startup file in my App.xaml from StartupUri="MainWindow.xaml" to the code below, hoping that this all I needed to do.
<Application x:Class="MVVMPrototype.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MVVMPrototype.View.MVVMPrototype.xaml">
<Application.Resources>
</Application.Resources>
However, I then got the following error stating that I need to set the build property to resource as show below. Previously it was set to Page, which is the same as what the MainWindow.xaml was set to.
After doing that I then have a compiler error stating that InitializeComponent(); does not exists in the current context. The only time I know this to happen is when the class / namespace is out of sync between the .cs file and the .xaml file, but this is not the case.
I have had a look at the StartupUri documentation but I have not found it to be that helpful. I am sure it is something silly I have overlooked. And yes, I have clean and built the project several times with no luck. Does anyone know how to solve it?
UPDATE: Seva Titov has solved the first part. But now I have a TargetInvocationException with an inner exception of System.Security.SecurityException with the picture below (open the image in a new tab if it's too hard to read):
Any ideas for this one?
StartupUri specifies relative file location, and you seem to have a file MVVMPrototype.xaml in folder View, so your code should look like this:
<Application x:Class="MVVMPrototype.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="View/MVVMPrototype.xaml">
Okay, I have now figured out what is happening.
As pointed out in the answer by Seva Titov the StartupUri should have the value of "View/MVVMPrototype.xaml".
The InitializeComponent(); error where it was stating it was not defined in the current context was because of the build action on the properties of MVVMPrototype.xaml was set to resource, not page. Originally I thought adding a new Window control solved this, but it turns out to be a red herring since the build action was set as page on the new control - I think that is the default.
The last error with the TargetInvocationException and the inner exception of System.Security.SecurityException I fixed by toggling the "Enable ClickOnce security settings" under Project Properties -> Security to generate the app.manifest.
This gave me the final error that it is unable to find the manifest signing certificate, so I just unchecked the "Sign the ClickOnce manifests" under project properties -> Signing.
This solved my issue. Hope it helps someone else. Please note I have little, if any, knowledge of deployment / publishing, and I am not sure how the last two will effect this.
in the App.xaml
StartupUri="/ProjectName;component/Folder Or Path/File Or Page.xaml">

How do you store an image in a Silverlight 3 custom control?

I have created a custom control that will have a bitmap image in it. My project structure is:
Solution
ProjectName
Resources
Actor.png
The XAML I am using is:
<Image x:Name="ActorBitmap" Source="ProjectName;component/Resources/Actor.png />
However, this is not working. I have the build options on the picture set to "Resource" and "Copy Always". Can someone please explain what I'm doing wrong ?
Thanks,
Scott
I believe you just need a leading forward slash before the resource location:
<Image x:Name="ActorBitmap" Source="/ProjectName;component/Resources/Actor.png />
There is also no need to set copy to output directory to "Copy Always". Because the build action is set to Resource the image data will be in the assembly itself, and you will not be referencing it from the output directory.

Embedded images not showing when in a UserControl

I have an Image control that contains a path to an embedded image (build action 'resource').
<Image Source="Assets/images/image.png" Stretch="None" />
If I add that to a container in my MainPage.xaml the image appears fine. When having the same image in a UserControl as shown below, and then adding an instance of that UserControl on MainPage.xaml the image does not appear.
<UserControl x:Class="HomePage.Views.SimpleUserContol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid x:Name="LayoutRoot" >
<Image Source="Assets/images/image.png" Stretch="None" />
</Grid>
</UserControl>
Can anyone shed any light on why this happening and maybe point me in the direction of a solution.
Cheers, J
(I'm working in Silverlight but would think the same thing probably happens in WPF)
EDIT:
Setting
<Image Source="/Assets/images/image.png" Stretch="None" />
works fine when setting the build action to 'Content' however it doesn't work when using 'resource'. The problem is definatly it's relative position in the file structure as add ../ works fine. I'd still like a solution to get an image from the assembly if possible
You have to reference it as a resource, not just the path.
This is how it is done in a WPF application:
<Image Source="/MyAppName;component/images/image.png" Stretch="None" />
The original image is located in images/image.png
Note:
I have no experience in SilverLight, but you said it is probably similiar in WPF, so I suggest this...
You're using a relative path to the image. If your UserControl is located in a subdirectory, the relative path is not valid anymore. You should use an absolute path like "/Assets/images/image.png", or "pack://application:,,,/Assets/images/image.png" (use this last version if your UserControl is going to be used by another assembly)
If you needed to change its build type from Content to Resource, try building clean. I had everything exactly right (path to other project's resources, build type, et al.) yet it didn't work until I added a new image to the images folder, which perhaps cleared out the old statuses.
In fact, manually delete the bin and obj folders in the project where the image is.
I think the problem is related to the "virtual" namespace your image got when it's embedded in ressources (from the logical path to it) and the difference with your usercontrol namespace.
using a '/' to get to the root of the site works only if the root of the site is not within a subdirectory. ie: an admin site as a subdirectory to the main site (http://www.somesite.com/admin). In this case using '/assets/images/image.png' would then go to the parent site first. You may be able to reference the image like so: '~/assets/images/image.png'
I've also found the when an image has special characters that require encoding in the file name, like "+", which gets encoded %2b can cause the problem as well.
I suggest renaming any images that might have contain escape characters.
I had this problem and I tried moving the image in and out of the Canvas, and up and down the Canvas container. I even tried manipulating the panel.zindexes to no avail. I was using source= "/images/map.jpg". The image folder was created by right clicking the project and adding folder, so it was below the resources folder.
Solved: I navigated to the images folder in windows explorer and used alt+d, and ctrl+c to copy the file location with these \ instead of these / and voila it worked.

Resources