Can't find ResourceDictionary in Add New Item Window - wpf

When I create a new WPF project I can find ResourceDictionary in Add New Item Window.
But I've another Project I can't find that and I don't know why.
UPDATE:
The project was for .net 3.5 originally, but now it also has a version for .net 4.0. It means there're two .sln files (one for 3.5 and the other for 4.0) both for the same project.

Add the following line to Project.csproj
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
It should be a child of the <PropertyGroup> tag, like so:
<Project>
<PropertyGroup>
....
<ProjectTypeGuids>{guids};{go};{here}</ProjectTypeGuids>
...
</PropertyGroup>
...
</Project>
This post does a good job of explaining why this works.

First of all, I hope you realize this shouldn't stop you since you can easily add any file you want to a project, either from your file system or by copying it from another project. The Add New Item window is just for convenience.
Secondly, when you added the new project to your solution, which project template did you choose? The project template determines the initial set of referenced assemblies that project has. A WPF project makes references to the WPF libraries (WindowsBase, PresentationCore, etc.).
Visual Studio uses your referenced assemblies to generate the possible items you see in the Add New Items dialog.
So I'm assuming you added some other type of project, such as a basic Class Library. You could manually add the references to the WPF assemblies using the Add Reference dialog. Or you could re-create the project as a WPF Custom Control Library.

Close the project. Create a new project that is of the type of WPF project you would have used(or use existing one). Then open the .csproj file of WPF project in text editor. Find the ProjectTypeGuids element.
Open your existing .csproj file in notepad. See if it has ProjectTypeGuids element. If it does, append GUID(without the ProjectTypeGuids) from WPF project in your existing project. If your existing .csproj file doesn't have ProjectTypeGuids element in it, copy ProjectTypeGuids from your WPF project together with GUID and paste it in your existing project in the first PropertyGroup element.
Reload your project in Visual Studio. You should be able to add all the WPF file types now.
I believe the GUIDS are the same for everyone so the values you need should be: {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} ... this should save you the step of creating a new project.
So if you have a class library project, just add
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
To the first PropertyGroup element in your .csproj file.

Related

How do I share an image among WPF projects?

I have a solution with a number of WPF projects and UserControls in those. Among these is a Core project which contains resources shared by all the other projects and all. One of these is an image file, which I have had to copy to all the other projects to use a relative file path for the Source property of various ImageEdit controls.
How can I set things up so that the image file only exists in the Core project, and I can refer to it in XAML in all the other projects?
Reference the project from the project that you intend to consume the image, then use pack URI to access it.
Something like this:
<UserControl x:Class="MyProduct.MyModule.MyView"
.....
<Image Source="pack://application:,,,/MyProduct.Core;component/Images/MyImage.png"/>
Build action for images should be "Resource".
Add the image to your Core project, then add as linked item to your other projects (Right Click Project -> Add Existing Item -> Select the file from your Core project then instead of "Add" you need to click the triangle next to "Add" and say "Add As Link"). You can then reference it in the usual way from within your specific project...

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.

How can I make Visual Studio 2010's "Add User Control" create a WPF control?

I have a class library that I created using the "Class Library" project template. If I right-click on this library in Solution Explorer and select "Add > User Control", Visual Studio adds a WinForms UserControl. That's not what I want -- I want "Add > User Control" to add a WPF user control.
I've already added references to the WPF assemblies (WindowsBase, PresentationFramework, and PresentationCore), and I already have some WPF UserControls in this library, and everything compiles. My library does not have references to the WinForms assemblies (System.Drawing and System.Windows.Forms). But apparently the proper references are not enough of a clue for Visual Studio, because when I try Add > User Control, it adds the WinForms references to my project, and then creates a WinForms UserControl.
I can add a WPF User Control to my WPF Application project, and then move it into my library. But that's a pain, and I'd rather have it work properly in the first place.
I think I'm probably missing some kind of arcane XML element in my .csproj file that tells Visual Studio which designer to use by default, and if I add the right XML element with the right cryptic GUID, it will start working properly. If I could create a new WPF Control Library, I could probably compare the two project files and figure this out. However, I'm using Visual C# Express, which doesn't have a template for a WPF Control Library project, so I'm out of luck there.
What do I need to do to my Class Library's .csproj file so that VS2010's Add > New User Control will add a WPF UserControl?
There are sub-projects class ids in the project file that affect the Visual Studio context menus and how the project behaves in general. The easiest thing to do is to recreate the project as a:
WPF User Control Library
instead of a "Class Library". It is possible if you already created the project to edit in the sub-project class ids by hand by opening the ".csproj" file in a text editor such as Visual Studio itself but its easy to cause more damage than you fix that way.
I believe but haven't test that another type of library will also work:
WPF Custom Control Library
which is intended to hold other types of controls than UserControl objects but being a WPF sub-project type the context menus also work correctly for the use case you are describing.
Edit:
For completeness, I've just tested how to manually add the sub-project GUIDS. Add this line to the first PropertyGroup in the .csproj file:
<PropertyGroup>
...
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
Not tested with Visual Studio Express.

No creation of a WPF window in a DLL project?

Is there a reason why Visual Studio won't let me create a WPF window in a DLL project?
I "solved" it by creating a window in an Application Project and copying it to my DLL project.
I also found that I could just create a UserControl and change the base class to "Window".
But if I had to do it this way, it's maybe because I shouldn't do it...
Make sure the project type is WPF User Control Library when you create your project.
If it isn't then no sweat, just edit the csproj file and make sure the <ProjectTypeGuids> element under Project/PropertyGroup contain the following GUIDs
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Also, make sure you reference PresentationFramework and System.Xaml in your project, or you will not get any WPF in your code.
You can try adding new WPF User Control Item and change that to Window.
Add New Item->WPF->User Control
In XAML:
Change <UserControl> tag as <Window>
In CS:
Change base class from System.Windows.Controls.UserControl to System.Windows.Window.
I do it this way:
1) create "WPF Application"
2) remove App.xaml
3) change Project properties -> Application Output type: to Class Library (originally there is Windows Application)
Otherwise you will get errors:
"Library project file cannot specify ApplicationDefinition element"
"The project file contains a property value that is not valid"
What do you mean that Visual Studio won't let you create a WPF window in a DLL project? Do you mean that if you right click the project, there is no option to add a Window there?
If that is the case, I think that means that you created a project type that isn't a WPF project type. I encountered something similar a while back when I wanted to upgrade a WinForms project to use WPF instead - see this question for more information.

How to convert a winforms project to wpf project

I converted a Winforms project by hand-editing the proj file. Changed project type guids and added an application definition section, and now I can add WPF Windows, Pages, etc. to the project. One thing that doesn't work is, the files I added do not have autogenerated cs files, and every new window component are missing their InitializeComponent method.
Any ideas?
If you unload the project you can edit the project file by adding/replacing the following element in the first <PropertyGroup> element:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
This will change the project type to a WPF project.

Resources