WPF can't find part of path in ShadowCache - c#

I have an image in a Asset class library project. The build action is Content and the Copy to Output Directory setting is Copy Always because I want to be able to allow users to replace these files without my intervention.
The problem is, I'm getting a design-time error (everything works find on compile and run) that says "Could not find a part of the path 'C:\Users\Name\AppData\Local\Microsoft\VisualStudio\12.0\Designer\ShadowCache\oreaxsnr.cjd\orsr3pqd.wnf\Images\TestImage.jpg'."
I'm using Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); to get the path of the assembly, and then I concatenate that with a \\Images\\TestImage.jpg string. Thinking that was the source of the problem, I commented out everything related to getting that image, cleaned the solution, and rebuilt. It still has a problem with finding part of the path.
I went to the orsr3pqd.wnf folder and there's the class library .dll that calls the image, and nothing else. I deleted all of the ShadowCache folder after closing Visual Studio, restarted VS2013, and it immediately put two folders in the ShadowCache folder.
Maybe it's thinking there's supposed to be a resource in the .dll, even though I'm setting the build action to Content? Any direction would be helpful.

You should be able to reference it straight from the XAML if it is in a project that is part of the application like this:
<Image Source="/AssemblyName;component/Images/TestImage.jpg" />
If that doesn't work, then this should:
<Image>
<Image.Source>
<BitmapImage UriSource="pack://application:,,,
/AssemblyName;component/Images/TestImage.jpg"/>
</Image.Source>
</Image>
If neither of these work for you, then I'd also suggest setting your image file Build Action to Resource, rather than Content.

Related

VS/C# Equivalent of Java/Eclipse "resource folder"?

While using Java in Eclipse IDE, one can add a folder to the "Build Path" using the "Add Class Folder" option in the "Libraries" tab, which allows the resources in that folder to get compiled inside the application's jar file, rather than outside or not at all.
.
With this, one can get the resources inside the folder as a URL via the Class.getResource(String name)method. I am already informed about C#'s equivalent: Assembly.GetManifestResourceStream(string name) or Assembly.GetManifestResourceInfo(string resourceName) methods, but I am not aware of C#'s "Build Path" equivalence in Visual Studio (I am using 2019, if you wished to know). Could somebody please explain how I would accomplish Java's build path resource folder in C#?
(Note that I am looking to create a resource folder where anything put inside the folder would be considered an application resource. I am not looking for a way to add one or more resource files individually.)
Any replies would be greatly appreciated! :)
After a little research, I had found a solution for this problem. There are in fact two possible solutions to this issue.
.NET Core Solution
The first involves editing the .csproj file of your C# project. This solution is only available in .Net Core.
You can add this code snippet to your file and change the {PATH_TO_RESOUCE_FOLDER_HERE} folder to your desired folder.
<ItemGroup>
<EmbeddedResource Include="{PATH_TO_RESOUCE_FOLDER_HERE}\**" />
</ItemGroup>
Now any item placed in that folder will be considered an embedded resource Assembly.GetManifestResourceStream(string name) method.
Regular .NET Solution
The second method involves using a .resx file to encapsulate all of your resources
In Visual Studio 2019, you can create a .resx file by right clicking on the location in your project where you wish to add the file to, and navigating to Add > New Item (you may also press Ctrl+Shift+A). You can now navigate to the item that quotes "Resources File" and select it. You can now use this GUI to insert your resources (for a deeper explanation, click on this or this link. For use cases, see this MSDN).
The "Resources File" option
Note that this solution will also work in .NET Core.
I hope this answer helped you as much as it did me! :)
You just create a folder and name it as you like it, say 'Resources'. Add any file you want in there to be treated as a resource by your application.
Then navigate to the properties of every resource file (you can press F4) and in the menu you can choose what you want the compiler do with that file (Compile Action is the option name if I remember well). There you select the type as a resource, the namespace (your Build Path), and whether you like the file to be copied every time you compile your application, and so on.

Source file ...g.cs could not be found (from a downloaded project)

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

Image from WPF project's resource stopped showing

I added some images as resources to my WPF project, changed their Build Action to Resource in Solution Explorer and added them to my Main Window like this:
<Image ... Source="Resources/user.png"/>
Everything was working until I added and removed one project from my solution, then these images stopped showing both in design and runtime. The <Image Source/> tag in XAML editor and the Error List now show the message:
Could not find a part of the path "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Resources\user.png".
I tried restarting Visual Studio and the computer, and also cleaned and rebuilt it, but I still don't get the images.
When you use <Image Source="" />, the Source is relative to the XAML file path. The XAML file was on the root, and I moved it to a View folder. I didn't know about this until I read this comment.
The images are stored in a Resources folder inside the project, so in this case the Source property must start with a forward slash to point it to root, like this:
<Image ... Source="/Resources/user.png"/>

Debugger: no symbols have been loaded after renaming namespace

I've got a Silverlight application with one Silverlight project and a host web project.
I renamed the namespace of the Silverlight application where the App.xaml is in. I did this via a refactoring tool. Afterwards the debugger just ignored the breakpoints. It's popping out the error shown in the title of this thread.
So I thought that maybe the refactoring tool fails, so I recovered the project with the old namespace in and renamed everything by hand (find in files). The app works, still, but I get the same error.
What the heck is this?
It sounds like the xap file name changed and you need to update the javascript to reflect the new name change. ;-)
Okay I solved this problem. My refactoring tool renamed everything fine. Even the name of the actual .xap file, which gets generated. The only thing which the refatoring tool did not fix is this line:
<param name="source" value="ClientBin/OldXapFileName.xap"/>
This was the reason, why the hosting project loaded the old xap file, everytime is started the application.
#Andrei Schneider: Thanks anyway. Your comment got me to deleting the ClientBin folder. So I've been asking myself why my application crashes, even though the new xap file got copied into the ClientBin folder.

Loading Content images from Blend from separate project

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!

Categories