Windows Forms Designer and Wpf Designer for .Net Core

Windows Forms Designer and WPF Designer for .NET Core

Windows Forms - Visual Studio 2019 (16.5 Preview 1 - .NET CORE 3.1)

Starting from Visual Studio 16.5 Preview 1, there is builtin support for Windows Forms designer in Visual Studio. To use the designer:

  • You must be using Visual Studio 16.5 Preview 1 or a later version.
  • You need to enable the designer in Visual Studio. Go to Tools → Options → Environment → Preview Features and select the Use the preview Windows Forms designer for .NET Core apps option.

Sample Image

For more information:

  • Updates to .NET Core Windows Forms designer in Visual Studio 16.5 Preview 1.
  • WinForms and WPF .NET CORE repositories.

Keep in mind, in this release, many controls aren’t yet supported in designer.

Workaround for Older versions

Starting from Visual Studio 16.5 Preview 1, there is builtin support for Windows Forms designer in Visual Studio. But for older versions you can use the following workaround:

At the moment, there is a workaround for using Classic .NET Windows Forms Designer for .NET CORE Windows Forms projects.

The workaround relies on having Classic WinForms App and CORE WinForms app in the same solution, having the same root namespace.

For adding new designable items, every time which you want to add new item, you need to add it to classic project, then move the 3 generated files (.cs, .designer.cs, .resx) to the CORE app, then adding them as link to the classic app.

For editing, since they are available as a link in classic app, edit them in the designer of classic app. All changes are visible to CORE app.

Prepare solution to use Windows Forms Designer for .NET Core Winforms App

  1. Create a Windows Forms .NET Core App. (See the steps and requirements.)
  2. Open the project in Visual Studio and save the the project including solution.
  3. Right click on Solution and Add New Project → From templates, select Windows Forms project, and name it the same name as Core app + ".Designer" and click OK.
  4. In properties of the Classic framework project, set default namespace to same default namespace of Core app.
  5. Erase all exising files in both projects and save changes.

Adding a Form or UserControl

Every time which you want to add a new form or user control, you need to do the following steps:

  1. In the classic framework project, Add New Item
  2. Select Windows Form or User Control
  3. Do some changes in desginer, like resizing the form, so resx file will be generated and Save.
  4. In solution explorer, right click on the form and choose Cut.
  5. In core app, paste all the items.(form, designer, resx).
  6. In the classic app, right click and choose Add Existing Item.
  7. Browse open dialog to core app folder and choose those 3 added files and click dropdown near of add button and choose Add As Link
  8. Compile the solution.
  9. Renest the files in the classic app using Mad Kristensen's File Nesting Extension or by editing project file.

Now, whenever you need to use the Designer on one of the Core Form or UserControl files, simply open the linked files in the Classic Framework project with the Classic Windows Forms Designer.

WPF - VS 2019 (16.0.3 .NET CORE 3.0)

The GA version of the WPF .NET Core Designer has been released at the same time as .NET Core 3.0 and it comes with Visual Studio.

Reference:

WPF Designer is completely independent of the Windows Forms Designer. We released its GA version of the WPF .NET Core Designer at the same time as .NET Core 3.0 and it comes with Visual Studio. In Visual Studio version 16.3.0 we had an issue with the Enable XAML Designer property set to false by default. That means that when you click on .xaml files, the designer doesn’t open automatically. Upgrade to the latest Visual Studio version 16.3.1 where this issue is fixed. Another option to fix it is to go to Tools -> Options -> XAML Designer and check Enable XAML Designer.

NET Core 3.1 WinForms Designer incompatibility with WPF User Control

I finally figured out a workaround; the idea was sparked by the Microsoft Docs page for the ElementHost control:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.integration.elementhost?view=netcore-3.1

In essence, move the WPF control hosting from the main UI into a WinForms control library. Hence, the User control in this WinForms control library will be a wrapper for the WPF control. Here are the steps I took in my test VS solution:

  1. Remove the "true" entry from the main UI project file I had added during testing.
  2. In the new WinForms control library, add "true" to this project file underneath the "true" entry. This enables the library to be a bridge between the two UI frameworks.
  3. If the WPF control to host is in a dedicated control library (like mine), then add this project as a dependency in the WinForms control library.
  4. Create a new User control in the WinForms library, if there isn't one already.
  5. In the Designer, add a panel container to the control with "Fill" docking. I named my panel as "panelWpf".
  6. Here is where the Microsoft Doc comes in. In the code behind file for the WinForms control, I first added an ElementHost control and the WPF control as private global variables. Then, in the WinForms "Load" event, I set the ElementHost docking style, added the WPF as a child, and finally added ElementHost as a control to the "panelWpf" container. Below is the code from this file. "WpfControlLibrary31" is my WPF control library project, and "TestControl31" is the WPF control itself. Lastly, "WpfTest" is the name of the wrapper WinForms User control.
  7. After building the WinForms control library, it appeared in the main UI project's Toolbox, and I was able to add it to the form like any other Windows Forms control. The next steps will be to add event handlers, getters, setters, etc. to the control for the needed interaction.
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WinFormsLibrary
{
public partial class WpfTest : UserControl
{
// ElementHost for the WPF control
private ElementHost host = new ElementHost();
// WPF control to be hosted
private WpfControlLibrary31.TestControl31 uc = new WpfControlLibrary31.TestControl31();

public WpfTest()
{
InitializeComponent();
}

private void WpfTest_Load(object sender, EventArgs e)
{
// set the docking style for the ElementHost
host.Dock = DockStyle.Fill;

// add the WPF control as a child of ElementHost
host.Child = uc;

// add the ElementHost as a control of the panel container
panelWpf.Controls.Add(host);
}
}
}

Thoughts:

  1. Some may wonder why I used the panel container. And in this simple experiment it was overkill; I could have simply docked the ElementHost to the control itself. However, if the WinForms User control has a more complex design, then the panel will be a placeholder while still allowing use of the Designer. Also, if a border or similar design is needed around the WPF control, then this should be possible with the panel.
  2. Having the ElementHost and WPF control object as global allows access from all the control's methods, obviously, just like any controls added in the Designer itself.
  3. The WPF control to host does not need to be in a dedicated WPF control library project. If it is a pre-existing WPF control (e.g. MediaElement), then use it for the global WPF object.
  4. This WinForms control library is what I have been needing to consolidate and improve efficiency of my custom controls. So this issue with the .NET Core WinForms Designer turned out to be a blessing in disguise.

What are your thoughts? Thanks for the brainstorming help!

Does Winforms (.NET Core 3) support all .NET Framework controls

Does Winforms (.NET Core 3) support all .NET Framework controls?

No, it doesn't. In fact in .NET Core 3.1 some of the outdated Windows Forms controls like DataGrid, ToolBar, ContextMenu, Menu, MainMenu, MenuItem, and their child components were removed.

You need to use DataGridView, ToolStrip, ContextMenuSrtip, MenuStrip and their child components instead.

To see a list of removed controls and required actions for upgrade take a look at:

  • Breaking changes in Windows Forms/Removed controls

I've noticed that not all the controls show up in the designer

Right, the design-time support is still under development and some of the controls and feature are still not available through designer. However if you don't want to use designer, you just need run-time support, use the controls which are supported by .NET CORE 3.1 but don't have design-time support.

Should we just wait for more mature releases or just use .NET Framework?

At time of writing this answer, if you need to use designer on a regular basis, it's still not recommended to port your application to .NET Core. While there is builtin designer support in VS 2019 Version 16.5 Preview 1, but the designer is still under development and some important features like data-binding, some of container controls, localization, MenuStrip and ToolStrip, Visual inheritance and so on ate not available.

To see the list of under development features take a look at:

  • Updates to .NET Core Windows Forms designer in Visual Studio 16.5 Preview 1
  • You may also want to take a look WinForms on .NET Core Roadmap
    .

.net core in visual studio works with console app but not with wpf or winforms

16.0.0

Update to 16.7.2. Core 3.1 is not in preview and has not been since late 2019, but some of that support followed the initial release of VS 2018.

Also ensure you have installed the correct workload with the installer (.NET Desktop Development).



Related Topics



Leave a reply



Submit