Why can't I set the Image Source in WPF app? - wpf

I'm just learning WPF. I'm using VS 2008. I've added an Image control to my XAML, and I added a GIF file to the project. If I use the XAML Designer's property panel to set the image's Source property, the drop-down list for the Source property contains the following:
C:\Sample Project;component/pinHorizontal.gif
There are several problems with this:
My project, named "Sample Project," is not in the root of my drive.
Why is ";component" in there?
If I select this value as given, I get the error "Property value is not valid" (yeah, no kidding).
If I go into the XAML source and set the Source property manually, like so:
<Image Name="PinImage" Source="pinHorizontal.gif"/>
The XAML Designer gives me this error:
"The file pinHorizontal.gif is not part of the project or its 'Build Action' property is not set to 'Resource'."
Why is this task so difficult? How do I assign an image source?

Have a read through this article - though be warned it's not particularly light reading :-)
I suspect what you want for your image path is pack://application:,,,/pinHorizontal.gif. if your image is set to a BuildAction of Resource, this will work fine.

Right click on the image file(pinHorizontal.gif) in the SolutionExplorer, Go to properties, here is the 'Build Action' property, you need to set it to 'Resource'. Hope it helps!!

This post is an "and also"... not an answer to the original question.
I just thought I'd document it in case some other poor booger has this problem in future... and googling that error message finds this SO question... so here's a good place to do so.
I've been creating new icons (actually bitmaps) and adding them "on the fly" to my Visual Studio 2008 project (i.e. Add ~ Existing item). The IDE has been reporting file ${filename} is not part of the project or its 'Build Action' property is not set to 'Resource' errors on my XAML page... but my project builds and runs OK... so what gives???
I tried everything I could think of the get visual studio to refresh it's view of the filesystem, to no avail.
Well, I've just dicovered that restarting Visual Studio "refeshes" its cache of filesystem/build-output contents, which makes those annoying non-errors in the IDE go away. So I add a batch of icons, mark them all as Resources ~ Copy if newer, build, and then restart the IDE, and it's all good.
Cheers all. Keith.

In the Solution Explorer, select your project and right click rebuild the solution, and try to add the image again, it should work fine.
--or--
under the Build tab select rebuild the solution, and try to add the image again.

You can use this piece of code in the XAML to load an image from the absolute path without setting nothing. Use DecodePixelWidth or DecodePixelHeight to save application memory.
<Image>
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="C:\image.png" />
</Image.Source>
</Image>
Hope it helps!

I was having the exact same problem. I just manually typed in the location of the file when the Choose image box pops up and that worked for me.
I opened the Choose image box again after I got it to work and noticed that file:/// was now in front of the file location. I know next to nothing about Visual Basic (which is what I'm using) so there may be a good explanation for this, but I don't have it.

Related

LiveCharts GeoMap WPF

I am trying to replicate this LiveCharts GeoMap in WPF but having an issue with the source file of my xml map :-
https://lvcharts.net/App/examples/v1/wpf/GeoHeatMap
I can produce it fine under WinForms but need to create a working version under WPF, if anyone can assist or point me in the right direction, I’m sure it’s something obvious that I’m missing?
The error I get states that the source path for my xml file doesn’t exist, when it clearly does.
If I follow the example, I create a user control, copy in the xaml plus the code behind, as per the website, then in my main window xaml, make reference to the user control. It’s at this point that it can’t load my main window as it can’t find the source file that I have referenced in my user control. In winforms I can reference it using source=#“c:\temp\world.xml” in code behind but cant do that in the xaml either. Where am I going wrong! Thanks
I had the same issue. It seems as if the NuGet package is outdated (2017).
Try to clone the LiveCharts repository and build it yourself.
In the properties of the XML file:
set Compile Action to none
In the Copy to directory:
set it to Copy Always
My Visual Studio is in Portuguese, but I think you'll get the idea.
If you're interested, there are detailed maps for each country/region; go to https://github.com/Live-Charts/Live-Maps.

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

XamlParseException on setting image from resources

i got some trouble. I just implemented the Microsoft Ribbon Framework into my application. Now i wanted to set an icon for one button. So i wrote:
<Ribbon:RibbonButton Header="Create Project" LargeImageSource="pack://application:,,,/Resources/document_empty.png" />
But everytime i start the app, i got an "XamlParseException" in this line, with the inner exception: "File not found".
Why the hell is this so? I don´t understand, why it doesn´t find the image. It´s just placed in the resources of the assembly. The designer show´s it and the code doesn´t get highlighted as it was an syntax error or something like that...
Anyone who has this problem too?
Thanks!
As you haven't specified exactly, I assume you are using the Properties\resources.resx part of your application? If so this is not the desired way for a WPF application.
See this post on SO
Instead create a folder in your assembly, place your images in there and set its build action to resource.
See picture:
In your Markup use the packUri:
pack://application:,,,/images/IMG.jpg

Visual Studio 2010 Designer Error on Run

I am using using VS2010 and if I have a form open in designer mode and run my application the designer tab will no longer show the form designer but instead an error will be displayed (and it is only fixed by restarting the IDE) saying:
"To prevent possible data loss before loading the designer, the
following errors must be resolved:"
1 Error:
"The designer could not be shown for this file because none of the
classes within it can be designed. The designer inspected the
following classes in the file: ##### --- The base class ##### could
not be loaded. Ensure the assembly has been referenced and that all
projects have been built"
I then shows the following call stack:
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
Any help is greatly appreciated this is really annoying.
Thanks,
Joel.
I get this visual studio bug too now and then, and I deeply ignore the error text, instead I do the following:
Close the Design-tab
Reopens the Design mode by double click in Solution Explorer, or by right clicking Source code tab and select View Designer
Suddenly everything works again!
If not helping, you may have to change bullet 2 into:
Close and restart Visual Studio.
Maybe this can help you out.
I usually close the visual form, rebuild the solution, right-click then select "view designer" in the form code.
Very, very annoying. I am thinking of dropping back to VS2008.
Close the form. Clean the solution. Rebuild the solution. Reopen the form. Worked for me when nothing else would.
I had this same issue and I was able to resolve this by creating new project and then compiled and run the project and then I imported all the files and ran the project again and automatically it was working again did nothing extra.
I'm able to avoid restarting VS by doing the following
Add a new user control
Drag and drop some of your custom user controls on to it (If it gives you an error, build the solution again).
Reopen your control.
In my case, I have a winforms project with several custom controls that are used by other custom controls. Whenever I open some of those custom controls, I get a the "The base class ..." error. Adding a new custom control, building the project and then adding some custom controls from my project to the new custom control allowed me to open the custom controls that were giving me the "The base class ..." error.
UPDATE: I think I found the problem. My controls were not 'added' properly to the csproj file. In the csproj file, the files for partial classes of UI controls/components need the 'DependentUpon' attribute.
E.x.:
before:
<Compile Include="Windows\Forms\DataGridView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Windows\Forms\DataGridView.Designer.cs" />
after:
<Compile Include="Windows\Forms\DataGridView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Windows\Forms\DataGridView.Designer.cs">
<DependentUpon>DataGridView.cs</DependentUpon>
</Compile>
I had a situation where a custom user control appeared to be creating the error (not sure why) so I removed references to the user control from the form and the error went away.
It seems that after installing SP1 the problem has gone away.
Thanks for your help everyone.
I had the same problem using A control With Generics
MvpUserControl<Presenter,IViewMode> : UserControl
what I do it's Remove the Reference and Add again, Clean and Rebuild the Solution I Hope this can be useful for anybody else
I had the same problem with VS2010 SP1. Finally using Windows Update I saw some updates for Visual Studio and .Net, I installed them and is not happening any more.
Old post, but for those whom may find this...
Just ran in to this error and for me it was relatively simple fix.
Found that it may have something to do with the names of your classes, and renaming the problematic class to a higher order. That is the alphabetical order it appears in the assembly (Where A is higher than Z).
MSDN Article
Good luck.
This error occur if the Form class is not a first class in the file, for example if there is some helper class at the beginning of the file.
To solve this issue, move all other classes except Form class to the bottom of the file.
Don't code in Form1.Designer.cs. Move your logic to Form1.cs (hit F7 on Form1.cs [Design] tab).
"In the project file (.vcxproj), locate the entry for the target
Framework version. For example, if your project is designed to use the
.NET Framework 4.5, locate
v4.5 in the
element of the element. "
(Microsoft)
In my case the "v4.5" didn't exist so I add it, and everything is good now.
I tried clean solution and re-build solution and worked for me.
Hope this help!
This solution works fine, please follow these steps below to solve your problem:
Check the reference if load correctly
Clean the solution and rebuild again
Clean the project and rebuild again
Clean your project and open it again
I was login as administrator in visual studio . I just close my visual studio and again open it without run as administrator and my problem is solved
I ran into this today after upgrading VS2019. I went to properties, configuration tab, and set the projects to Configuration = Debug, Platform = Any CPU. Then it worked.
I faced this issue.
To prevent possible data loss before loading the designer, the following errors must be resolved:
I found solution for this problem:
Close all open tabs, and refresh (sync active documents) the solution.
For more information, you can see this video : https://youtu.be/Q3x2HBd7BDs

Why isn't Expression Blend rendering my User Control? It's only showing XAML

I'm opening valid XAML within my VS2008 solution in Expression Blend 3 and it is only showing XAML when I try to open individual XAML files. My solution/projects all build and run correctly.
When I go to View -> Active Document View the Design View, Split View and XAML View options are all grayed out... which doesn't make much sense.
I'm not much of a Blend user, but this has never happened before, and I'm coming up blank for how to fix it. Any ideas?
How did you create your project ? Did you use the "Class library" templates in Visual Studio ?
I remember I had a similar problem. You can create a new WPF project and compare the CSPROJ with the one not working in Blend. You should be able to see the differences and add the missing lines in your CSPROJ.
HTH
For me, the problem was solved by changing the target framework of the project - I was using 4.5, but Blend could support max version 4.0 - try that...
open the project file in notepad and add the following line within the <PropertyGroup>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Found the solution here -> link text

Resources