Why does the Image element give me a design-time error with "add as link"ed images? - wpf

I need to display an image, which I've done without problems before, but today I decided to be tricky and use "add as link" instead. Well, now I get:
The file Images/hello.png is not part of the project or its 'Build Action' property is not set to 'Resource'.
Wait... its Build Action is set to Resource. I've seen a Silverlight solution that involves the usage of Merged Dictionaries to share files between Silverlight and WPF projects, but it's not clear to me that this would even apply to my WPF + Image issue.
Has anyone solved this problem before? I could make copies of all of the images, but that seems a little silly if I have a shared repository with clip art and the like.

Dave,
I've just tried to add image as a link to plain WPF application. Build action is "Resource" (don't confuse with "Embedded Resource"). I've added it to the root, and refer to it as <Image Source="/file_name.jpg"/> - all works fine.
The message you have is it compile or runtime? If it's a runtime, how do you refer to the image? Do you see it in Reflector, when you open your assembly (it should be under Resources folder)?

I have images in one assembly which I want to share into another. I've used Add as Link in my second assembly. In the project where the actual image files are located they are in a Resources\Images folder. In the project which links to those files the links are also in a Resources\Images folder. At runtime a XamlParseException claiming "cannot locate resource" is thrown.
My xaml which is referencing the image is in a UserControls folder.
In the project which actually contains the images the path ..\Resources\Images\Blah.png works fine as expected.
Opening the DLLs in Reflector makes it obvious that in the assembly with the linked images holds the images at the root level - the compiler is not respecting the folder location. So in the project with the linked files I have to use ..\Blah.png to find the resource.
Surely a bug in the compiler?

Related

VS2012 - Add WPF existing user control to project

This seems like it should be pretty simple but I can't seem to make it happen. Lets say I have an existing project with a user control named uc1. I would like to use this user control in another project. I right-click the project name in the solution explorer and select add>existing item, change the drop down to all files and select the files uc1.xaml and uc1.xaml.vb. This of course adds the files to the project but there is no correlation between the xaml and the code behind file and there is no way to use the control. What is the proper way of doing this?
Reed's answer is a good architectural one. If you plan on creating a control that you will reuse in many projects then it's best to use a control library.
Your original question is valid in some situations though. Say you have some source code from the Internet that you've unzipped to your drive. This project contains a .XAML file and its linked .vb file that you want to add to a project.
As you seen, the Visual Studio Solution Explorer doesn't link the files when adding with the "Add Item" dialog. I think this is a bug. I find that if I reload the project, the affiliation is added.
Here's a workaround I use. I drag the files from Windows Explorer /File Explorer onto the project in Solution Explorer. That works correctly the first time.
This of course adds the files to the project but there is no correlation between the xaml and the code behind file and there is no way to use the control. What is the proper way of doing this?
Normally, you'd add a reference to the other project, and use the UserControl directly.
This allows you to build a single project with your UserControl, and use the resulting assembly (DLL) in multiple projects without duplicating the code.
If you want to reuse your user controls you need to create a new project and choose "Class Library" from the list of available projects. When compiled this class library can easily be used by any number of other projects and solutions simply by adding a reference to compiled DLL created when you build this class library.
Edit: As mentioned in other answer it's "WPF UserControl Library", not simple "Class Library"...
You just need to add the .xaml file and VS should auto add the code behind(nested). I've seen this not work a few times and as #Walt Ritscher said this is probably a bug.
I found simply restarting Visual Studio and reloading my solution worked.

unblocking Ribbon WPF assembly for use in VS2010

I'm trying to open the sample Solution for the Microsoft Ribbon for WPF, but I get this error about being unable to load metadata. The solution on that site, and the one linked in the error, is How to: Use an Assembly from the Web in Visual Studio. However, I don't see any 'Unblock' option when I check the Properties dialog for RibbonControlsLibrary.dll in C:\Program Files (x86)\Microsoft Ribbon for WPF\v3.5.40729.1. This may be because when I downloaded the Ribbon stuff, it wasn't the library directly, but rather an installer for the library. How can I get rid of this error so I can actually see the design view of the sample XAML files for the Ribbon WPF?
Here is the full error message:
Unable to load the metadata for assembly 'RibbonWindowSample'. This assembly may have been downloaded from the web. See http://go.microsoft.com/fwlink/?LinkId=179545. The following error was encountered during load: Could not load file or assembly 'RibbonWindowSample' or one of its dependencies. The system cannot find the file specified.
According to the error message, it's not RibbonControlsLibrary.dll that's at fault, it's RibbonWindowSample. Have you unbolocked the solution file and files that form the project? If RibbonWindowSample came pre-compiled, you may need to re-build the solution to generate compiled assemblies that don't have the "web mark" on them.
Did you download a zip file? You need to unblock the zip before unzipping it. Once it's unzipped you won't be able to unblock the individual files that were in it.
Here's what to look for at the bottom of the properties dialog.
I was seeing the metadata error before building the Solution--it shows up when I first load the Solution. I tried building it just now and it built fine, plus the design view shows the XAML window. I don't know why I see the metadata error when it first loads but it goes away upon building.

Why Resource (.resx) file added on merely changing Form size and on adding button which is not resource

1- Resource files suppose to be added on adding some resource in application like image or audio or video etc. But if I just change size of form a .resx file incleded under that particular form. Changing size of form does not add any resource so why this .resx file?
2- I dropped a button on form and a resource file is included; again this button is not some kind of resource, it is object having its creation information in designer file?
3- A resource file added on dropping button on form but if I delete this resource file and run application it compile and run with NO error and button is still there. If this button has any relation with resource file then there must by some kind of compile or runtime error AND if .resx file has nothing to do with button then why it was added?
I am using VS 2008.
EDIT:
I have added a picture box and added an image in this now this is resource. i deleted the From1.resx file and I was expecting an error but NO ERROR AGAIN. In designer file picturebox referring image from properties folder which has Resources.resx file. Double click on this and image is there. SO WHAT WAS THE PURPOSE OF From1.resx FILE?
Thanks in advance for the help
AFAIK there isn't a strict requirement, but it is probably one of those things where it is simpler (read: less brittle, more predictable, cheaper to implement, whatever) to generate the resx if there is even the suggestion that one could possibly be required (even if it turns out to be trivial) than it is to fully track the nuances of when one is absolutely demanded.
I agree; in the scenario you describe no resx is required, but it isn't going to upset me.
(it still happens in VS2010, btw - but then I suspect you shouldn't hope for much change to winforms in VS2010)
It seems that the resx file for that form is most likely tied to localization. I.e. For localizing Text content such as the text on the button. When you add a resource such as an image file by default it is placed in the applications resx file that is located in the My Project folder. So if you have the form or projects language set I think that it will always create a local resx file for that form when you add a control that could be localized.

How do I include specific controls from System.Windows.Controls in my Silverlight app?

Microsoft has a handy reference of the various controls in the System.Windows.Controls namespace and which controls are included in the runtime.
For any controls listed here that aren't included in the runtime, you have to include the reference to the System.Windows.Controls assembly and select "copy local" in Visual Studio. So that's what I'm currently doing and it works.
However, by including this DLL I'm adding almost 80 KB to my XAP file (and my XAP file is only 110 KB). The only control I'm using that requires it is the GridSplitter. Is there a way to rebuild a condensed version of this assembly that includes the GridSplitter only?
It looks like I just found the answer for Silverlight 2...
Source Code for Silverlight 2 Runtime and SDK Controls
And I think that'll work for me for now. I should be able to go through the project and exclude all the unnecessary bits from the project and do a rebuild.
I'm still using Silverlight 2 but I will be switching to 3 in a couple months. And I can't seem to find the same thing for Silverlight 3. Maybe the source is not available yet for Silverlight 3?
UPDATE:
So I got this working after jumping through some hoops. At first I downloaded the source project from Microsoft and then just started removing stuff. But then I noticed that I couldn't build the solution. In fact I couldn't build an unmodified version of the solution either. I was getting errors about strong name verification. I tried unchecking strong name signing from the "Signing" section of the project properties but I got the same error. I even added the assembly to the strong name verification skip list and finally got past the error. But when I tried to include the new assembly in my Silverlight project and run it in a browser, I'd get a blank screen (the Silverlight control never loaded). I'm still not sure why but I'm sure I was missing something.
Finally I just created a fresh project. It seems I should've done this from the beginning. I just needed to create an empty C# Silverlight Class Library for the GridSplitter. Then I started adding the source files necessary for the GridSplitter to this project and then I set the namespace and assembly name to System.Windows.Controls. After building this project, and referencing it in my Silverlight app it finally worked. The result: a 60 KB savings.
I wrote a little more about this here:
http://regexhero.net/blog/2009/09/optimizing-silverlight-xap-file.html
In case anyone wants them, I've got 4 of the controls from the SDK right here...
http://www.silverlightxap.com/controls/tagged/silverlight-sdk

Linked Themes/Generic.xaml Files will not work in visual studio 2008

i have a project that i am doing and i need to share the code between silverlight and WPF Assembly problem is that even though the wpf assembly is the owner of that file
and the silverlight assembly only has a link to the file, all of the build actions are page everything is correct. if i make the silverlight assembly the owner then silverlight works and wpf doesnt, and currently with wpf being the owner i dont get any errors at all it just never styles the control like it cannot find it..
Note: both projects exists in the same solution.
this scenario builds and runs fine
wpf project
|__Themes
|__Generic.xaml
|__SomeControl.cs
this scenario builds and runs but will not display the control
if i change them from linked to normal it will work fine.
i just want to share this source code and not have multiple versions of the same file floating around.
SilverlightProject
|__Themes
|__"Linked"Generic.xaml
|__"Linked"SomeControl.cs
sorry for my corny Tree view representation
+++++++ UPDATE +++++++++
i have noticed when using any linked file regardless of if it is silverlight or WPF
the link file will not build into the Themes folder in the resource only the root.
i used reflector to see where my resources ended up after compilation of the assembly including the linked file and they ended up in the root , so with that being said. is there a way to prevent this or a fix for this if this is indeed non intended behavior .
i would really love to get this figured out as it has been driving me insane for a while now.
Silverlight XAML and WPF XAML do not have the same namespace - so they aren't directly reusable.
My mistake - you're right - now with Silverlight 3 the namespaces are the same:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml
What is the Build Action in the Property Pane for the XAML?

Resources