I have a VS2010 solution with 5 projects. Two of these projects are called:
MyResources
Application
The Application project contains all of the views and viewmodels for the application to run, and the Resources project contains all the images, resource dictionaries, etc for theming and skinning.
The way I have images set up in Resources, I have their build action set to Content, and Copy set to Copy Always. In my Application project, I reference these files using a pack URI. Example:
"pack://siteoforigin:,,,/Themes/DefaultTheme/BackgroundImage.png"
This works fine for me. The themes folder and all its subfolders are properly copied to the proper folder since I'm using siteoforigin://.
However, this breaks Blendability. When I load the project in Blend 4, I get errors.
If I use siteoforigin, Blend tries to load the images from the executing folder of blend.exe (In this case, C:\Program Files\Microsoft Expression\Blend 4 Beta\Themes\Default_Theme\BackgroundImage.png")
If I change the pack URI to the following:
pack://application:,,,/IQ.IQKiosk.Resources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "themes/default_theme/backgroundimage.png"
If I try:
/MyResources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\MyResources;component\Themes\DefaultTheme\BackgroundImage.png"
If I try:
/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\Themes\DefaultTheme\BackgroundImage.png".
So now I'm stuck. I'm not sure how to reference my images without having to embed them into the resource project, and I can't hard code the directory because my other coworkers have the project in different folders as well.
I tried to think of a way to get the proper URI to the proper locations of the files (relative to the .sln file would be awesome) or have the files get copied over to where blend compiles and executes the project. Alas, I am unable to figure out how.
Does anyone know the proper way to get these images to load?
I figured it out. Instead of using a pack URI, I just used
new Uri(Environment.CurrentDirectory + "\\Themes\\DefaultTheme\\BackgroundImage.png")
and it found the proper folder, since Evironment.CurrentDirectory gave me the proper bin folder where my themes were copied to.
Huzzah!
Related
I downloaded this project to play around with and get an understanding about wpf pages, however no matter what i do, i cant get it to build.
Source file 'C:\Users\mikeh\Desktop\How to Build, Manage and Navigate the User Interface of a WPF Application\C#\ApplicationNavigation\obj\x86\Debug\View\Page3.g.cs' could not be found.
I get this error for each xaml file in the project. I doublecheck app.xaml is set to ApplicationDefiniton, everything else is set to page.
I deleted the bin folder (although this folder did not exist in the downloaded zip)
How can i get this application to build? There has to be a way to force build this file without doing a build.
Assuming you downloaded the project and saved it to Desktop or My Downloads and instantly tried to open it, the problem may be due to a long filepath.
Rename the How to Build, Manage and Navigate the User Interface of a WPF Application folder to just Demo and try again. That should work.
https://code.msdn.microsoft.com/How-to-Build-Manage-and-fdd0074a/view/Discussions#content
I have two assemblies in my application one is where all the code is, the other holds all the images and extra resource files. It was working just fine until I added new images to the resource dll. Now it keeps coming up with MissingManifestResourceException and saying it can't find certain images. All my images are being compiled as Resources.
Fix this problem as right-click the Resources.resx file in the Solution Explorer and click Run Custom Tool. This re-generates the auto-generated Resources.Designer.cs file.
If the .resx file was added to the project manually, the Custom Tool property of the file must be set to "ResXFileCodeGenerator".
Rebuilt that project and then run it
I am trying to program an automatic Resource Loader for my project and so it is going well. But I am stuck on the loading of the sprites. I used this for checking the file exists
if (File.Exists(contentManager.RootDirectory + "/Sprites/" + spriteName + ".png"))
However it never finds the file, even though the file exists in the Content of the project. I have checked the folders and found that the .pngs are replaced by .xnb files. So I tried changing the file extension and then was given an error in the Sprite Load function
return contentManager.Load<Texture2D>(assetName);
It only happens if I change it from being a typical image file (png, bmp, etc). Can someone explain why this is the problem and how I can get around this? I have had no experience in the xna file extensions and can not find the answers I need. What I want is to load a new image into the project providing it exists in the Content Directory after compile
While on the topic, will the Sounds and Music be effected by the Compiler?
When you use a content project, the resources are imported, processed and write to disk again as xnb files.
If you want to dynamically load resources you have to compile files on the fly through the content builder or avoid using the content project.
Microsoft provides examples like this to load content dynamically: http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_2
if you want to distribute the application, the runtime xna is not enoigh, you need full xna framework and visual studio installed to work.
if you want the original files in the content project, you have to change their "copy to output folder" property to "copy always", and realize that the compiled results will be at "project folder\bin\debug\content"
I have multiple projects in a solution and I'd like them all to share one pool of graphics resources. I've tried this:
1 - Created project1, made its resource file public, added some graphics to it.
2 - Created project2, Alt+dragged Resources.resx from project1\Properties to project2 (not in the Properties folder)
3 - Add reference in project2 to project1
So, now all the images from project1 are available in project2. So far, so good. I can use them at design time just fine.
But, when I want to access them at runtime, I try this (in project2)...
Image img = project1.Properties.Resources.image14;
And that crashes with a MissingManifestResourceException.
What am I doing wrong here? Is there a better way I could approach this? All I'm trying to do is maintain all my graphics in one place, so if I add a resource, it becomes available to all projects in the solution.
Just built an example following these steps:
Create a class library do hold the resources (Project 1)
Create the consumer project (Project 2)
Add a resource file (GlobalResources.resx) in the Project 1 and add a resource item Information
Change the BuildAction of the resource file to Embedded Resource
Change the Do not copy of the resource file to False
Check if the Custom Tool of the resource file is set to PublicResXFileCodeGenerator
Add a reference to the class library (Project 1) to the consumer project (Project2).
Add the resource namespace reference wherever you want to use it.
Finally it is working: GlobalResources.Information
It should be simple.
Edit:
You are concerned about using an external resource file inside the design time property editor. Sorry to inform that there is no standard support for this :(
However, if you think that the benefits are greater than the effort:
Issue with shared WinForms resources across projects in Visual Studio
How do I get the Windows Forms Designer to use resources from external assembly?
Hope it helps.
Choose the referenced file in your solution explorer, then properties, then see what the "copy to output" property looks like. I suspect it's not set to "Copy Always" or "Copy if Newer" of which either should be fine.
Once it's being copied, let's also check to see where it's being copied. Is the output path for that particular item the same as where the program ultimately expects? Is it being copied to the bin\Debug of the correct project?
Make sure it's being copied to the path where the MissingManifestResourceException says it's failed to find the resource.
Finally, given additional information in our comments, I would also suggest you verify the following:
culture the resources are targeting. Check spelling and capitalization.
any culture settings of your build xml or publish xml.
culture setting(s?) of your host system that's running this code.
I have a C# project where I include some resources which I need to refer using the Uri-class. For this specific problem I have some shaders placed in a "Shaders" folder in the root of the project, but I've had this problem before with other files like images, etc. This far I've used the simple solution giving a fixed absolute path, and making sure the file is present at that location. Needless to say - this is not a good solution, and it won't work in the long run...
So, how can I use relative paths to refer my resources? I guess my question is twofold:
First; I don't want to refer the relative path from my Debug folder to the project folder. I want the file to be copied to the build folder. The shaders are included in the project, but obviously this isn't enough. How do I tell the project to copy the files when building? Or; what's the common way of solving this?
Second; when I have the "Shaders" folder copied to my build folder - what Uri syntax do I use to refer e.g. "myShader.ps" placed inside this folder? Can I simply say file:///Shaders/myShader.ps?
To get a Uri from your applications directory do:
Uri baseUri = new Uri(System.Reflection.Assembly.GetEntryAssembly().Location);
Uri shader = new Uri(baseUri, "shaders/test.ps");
Answer to first:
How do I tell the project to copy the files when building?
Add the file to the project, right click, select Properties and change Copy to Output Directory to Always Copy.