How to convert a winforms project to wpf project - wpf

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.

Related

Including UWP project in WPF project raises runtime errors

Project I work on need to show UWP control from UWP library inside of WPF window.
Here is the document page on which I had based my work (check section for adding custom UWP control):
https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/windowsxamlhost#add-a-custom-uwp-control
Had setup WPF window and it shows standard UWP controls (like button) at the runtime just fine. However, when I include UWP project, WPF window throws exception at start (at the moment of context initialization of EF).
Could not load file or assembly 'Windows.Foundation.UniversalApiContract, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
Sqlite version I'm using is 1.0.88.0 and EF is 5. But it doesn't make any sense that it clashes with these libraries?
Ffixed an issue on startup by updating SQLite package to 1.0.111.0 and EF to version 6.2.0.
Including UWP project in WPF project raises runtime errors
Derive from official document, I create sample project. When build the project, it throw many errors that lost assembly. After add the Windows.winmd file every thing work well. You could try to add the Windows.winmd file where in the C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd to WPF app reference.
Detail steps
Right click project References -> Add References -> Browse(file type all file)-> select Windows.winmd
And this sample project that you could refer this.

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...

Can't find ResourceDictionary in Add New Item Window

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.

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.

Resources