Could not find a part of path when using ImageBrush - wpf

I get that error if i try to run my "application". In fact that's an empty window with background.
<Window x:Class="NWN_Tsuki.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="831" Width="875">
<Window.Background>
<ImageBrush Stretch="None" ImageSource="pack://siteoforigin:,,,/Resources/Book.png" />
</Window.Background>
<Grid/>
</Window>
That's all my app. And i get error. If i set absolute path to image - all working fine.
I tried to google, i searched same topics here and tried to do what worked for others - there are no progress. Just one window. Just one background image in resourses added through wizard.
Visual Studio 2013, Windows 7 Ultimate x64

I haven't tried adding an image thru a wizard, what I do is create a "Resources" folder in my project. Then I add the image in said folder. Set the property of the image to Copy Always or Copy If New. Then do:
<ImageBrush Stretch="None" ImageSource="../Resources/Book.png" />
If anything, Book.png is just not getting copied.
EDIT: IMPORTANT Also make sure to change the Book.png's Build Action to Resource

First of all, I think you can just write ImageSource="Resources/Book.png" and it will work.
Second, if you want with "pack://", you suppose to write it ImageSource="pack://application:,,,/Resources/Book.png"
Yet another way - ImageSource="ProjectName;component/Resources/Book.png" where ProjectName is the name of the project where you keep you picture.
You can even concatenate all this if you need ImageSource="pack://application:,,,/ProjectName;component/Resources/Book.png"

I have experienced this. It turned out, the .png was corrupted. When I navigated to the folder in File Explorer, I saw the following
I think the mkelem is something to do with our source control, so maybe it was that which corrupted it, regardless, this is why some people may get this error message.

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!

App runs normally but has "errors" during design time: "does not exist in namespace" and "an error occured when finding the resource dictionary"

My application has certain errors, in its error box during design time, to do with it's resource dictionary. For example, in my Main Window a resource dictionary is called as follows;
<Window.Resources>
<ResourceDictionary Source="Resources.xaml"/>
</Window.Resources>
I get a blueline on the middle line saying "An error has occurred while finding the resource dictionary "Resources.xaml". Yet, the Resources.xaml is in the root folder of the project, and so is the MainWindow.
I am aware that there are similar questions like this out there, however the answers;
"Make sure Resources.xaml is set to build action: page"
"Make sure you are correctly referencing Resources.xaml"
were not helpful to me.
I have an additional problem in that, within the resource dictionary, I have errors telling me;
the name ProductDatabaseViewModel does not exist in the namespace "clr-namespace:gate2software.ViewModels"
and similarly;
the name ProductDatabaseView does not exist in the namespace "clr-namespace:gate2software.Views"
in the following xaml;
<DataTemplate DataType="{x:Type vm:ProductDatabaseViewModel}">
<vw:ProductDatabaseView />
</DataTemplate>
In reality both of these are available exactly where they are stated to be.
Any suggestions for either of my two problems would be greatly appreciated.
make sure your path is correct? or try like dis path:
<ResourceDictionary Source=ms-appx:///CustomControls/Resources.xaml>
Have you tried to close the designer process (sometimes this works)?
Task Manager > Details > XDesProc.exe > end process. You will be able to reload it by clicking refresh in any VS view, but first rebuild all.
And by the way, Resources.xaml is for sure a ResourceDictionary type and not Page ? This kind of errors are shown also when you try to add a page as resource, not a ResourceDictionary.
I just ran into this same issue. My projects were targeted for a x64 build. I changed them to Any CPU See below and was able to see my view model data at design time.

Silverlight duration tooltip

I'm trying to increase the length of time that a tooltip displays in a silverlight application. I downloaded Silverlight.Controls.ToolTip from codeplex and I add it as a reference. Here is my code in my xaml:
xmlns:Controls="clr-namespace:Silverlight.Controls.ToolTips;assembly=Silverlight.Controls.ToolTips"
<Button Content="button content">
<Controls:ToolTipService.ToolTip>
<Controls:ToolTip DisplayTime="00:00:10" InitialDelay="00:00:03">
<TextBlock Text="tooltip"></TextBlock>
</Controls:ToolTip>
</Controls:ToolTipService.ToolTip>
</Button>
The code compiles. However, while running the code I get an error "XamlParseException occurred The attachable property 'ToolTip' was not found in 'ToolTipService.' Any ideas on what is causing this error? Thanks!
I had the same problem, here is how I solved it.
1) Go to: http://tooltipservice.codeplex.com/SourceControl/BrowseLatest
2) Download the Source Code
3) Copy "ToolTip.cs", "ToolTipService.cs" and ToolTipTimer.cs from
"branches\2.2.0\Silverlight.Controls.ToolTips\" into your own project somewhere.
4) Now point the xmlns to assembly on your own project where you have copied the source file into.
So basically, just use the source file instead of the dll and it will work. This has worked for me, hope it works for you.
Good Luck.
If you are referencing the ToolTip DLL from another DLL, try also adding a reference to it into you main project.
You have downloaded this DLL from web and have not unblocked it yet.
Right Click on the DLL, go to properties and unblock it.

WPF custom control XAML null error

Yet another strange WPF error:
I have a custom control in a simple XAML page. The project builds and runs perfectly, with no errors.
Here's the XAML:
<Window x:Class="Grapher2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graph="clr-namespace:Grapher2"
Title="Grapher" Width="800" Height="600">
<StackPanel Name="container" Width="700" Height="500">
<graph:GraphCanvas x:Name="graphCanvas" Width="700" Height="500" Background="#ddd" />
</StackPanel>
But when I try to view the XAML page in the XAML designer window, I get the message:
"Problem Loading--The document contains errors that must be fixed..."
When I hit the "Reload the designer" link, an error pops up in the Error List which says:
"Object reference not set to an instance of an object"
and the line starting with "graph:GraphCanvas..." is underlined.
I was originally developing on our development server, and there, everything was the same, except the error was:
"Request for the permission of type 'System.Security.Permissions.SecurityPermission (...blah blah... failed.)"
Again, everything works perfectly, except that I can't view the page in the XAML designer window, which is keeping me from developing the rest of the app.
I've tried cleaning, building, rebuilding, and all combinations of orders of these commands, with no success.
EDIT:
Please bear in mind this is my first custom control, and I could be doing something horribly, horribly wrong. Like I said, the app compiles and works beautifully, so I'm kinda mystified.
EDIT #2:
My derived canvas is not a partial class. From my understanding of how InitializeComponent works (What does InitializeComponent() do, and how does it work in WPF?), there is no auto-generated partial class that contains InitializeComponent linked to my control. Again, the control works perfectly without it, except for breaking the XAML design view.
I had a similar issue recently. Basically my understanding is that in designer some things happens in not exactly the same order as during run-time and some things that you would think could never be null actually are null during design-time.
I solved the problem this way: commented large parts of code in my control to the point there was no error in Cider and then uncommented them until I got the error again. Then when source of the error was localized, I surrounded problematic parts with
if (something != null)
{
...
}
Even when I felt that there's no way that could be null. And after some time I got rid of the error.
Not very "scientific" approach but it solved my problem. :)
Unfortunately, there are problems with the Cider designer in Visual Studio 2008 where this type of error is all too common. The version in VS2010 is vastly improved, but that's no comfort when you hit this issue in VS2008.
I'm not sure what "Grapher2" is but you might check to make sure that there isn't code in its constructor that can't run correctly when the designer instantiates it. Something like a database call would be problematic.
I've seen similar issues.
This is only a partial solution as it won't render in the parent in design mode, but it'll get rid of the error. It's the best solution I've been able to find so far.
In the Constructor of your custom control.
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
InitializeComponent();
Hope that helps, or maybe it'll help you find a better solution. If you do please post it so I can use it ;)
Edit
I get a different error when not using the code above:
Could not create an instance of type ''
So may be a different issue then what I've seen, but sounds like it might be related.

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