简体   繁体   中英

Windows Forms Designer and WPF Designer for .NET Core

I have recently heard that the alpha version of .NET Core (.NET Core 3.0) supports Windows Forms and WPF. But the Visual Studio Designer (and Visual Studio) doesn't support the .NET Core version of Windows Forms and WPF.

Is there any way to make the Visual Studio Designer(and Visual Studio) work with .NET Core 3.0 or Is there any other designer or IDE that I can use to work with .NET Core 3?

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.

在此处输入图片说明

For more information:

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.

For the WinForms project you can refer to this video

How to fix WinForms Designer not support .net core 3.0 visual studio 2019

Since this video give more details we need, we can be followed step by step. It is also based on Microsoft official github project dotnet/winforms

dotnet/winforms - Using the Classic WinForms Designer in WinForms Core

The related code (VS project) made by me is already pushed to github:

.netCore-WinForms_Designer


For the WPF project you can use XAML Designer as below:

Workaround to use the designer in WPF Core App

Reference:

dotnet/samples - WPF Hello World sample with linked files

Install vs 2019 Professional/Enterprise version

Firstly, you need to Install vs 2019 Professional/Enterprise version. Then installing .net core 3.0 SDK is needed. Now you can try to create a .net core WPF application,

1555775855179

After setting the relevant project name and storage path, it will pop up:

img2-BravoYeung

Check "Use preview SDK" under .NET core in VS options

After setting done, restart vs to take effect.

Use VS build-in template, create WPF Project named "CoreHiWPF" of .net core

1555775855179

1555775915106

Under the created solution, use VS build-in template to create new WPF project "HiWPF" of .net framework type

1555775957478

1555775984667

Now the file structure of the solution is below:

1555776045894

Update Assembly Name of .net core WPF "CoreHiWPF", make Assembly Names of two projects are the same

Right click the project CoreHiWPF , select Properties , then change its Assembly Name to HiWPF .

1555773128801

Then right click the project, click "Edit CoreHiWPF.csproj".

1555773253120

Add code as below:

1555773381960

  <ItemGroup>
    <ApplicationDefinition Include="..\HiWPF\App.xaml" Link="App.xaml">
      <Generator>MSBuild:Compile</Generator>
    </ApplicationDefinition>
    <Compile Include="..\HiWPF\App.xaml.cs" Link="App.xaml.cs" />
  </ItemGroup>

  <ItemGroup>
    <Page Include="..\HiWPF\MainWindow.xaml" Link="MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
    </Page>
    <Compile Include="..\HiWPF\MainWindow.xaml.cs" Link="MainWindow.xaml.cs" />
  </ItemGroup>

Ensure .net core WPF project CoreHiWPF is set as start up project

If the .net core WPF project CoreHiWPF is already highlighted, you can ignore then. Or, you need to select project CoreHiWPF , then right click Set As Start up project .

Try XAML Designer

Now close all opened files. Click the file MainWindow.xaml of project HiWPF . Then you can see empty WPF window in XAML designer now.

1555773860860

Modify the file MainWindow.xaml and related .cs if needed

Then, I add two lines to Grid of the file MainWindow.xaml : - One line is a Label which support Wrap - The other line is a Exit button.

Then I added the Click event to Exit button, added the Loaded event to Window .

调整

After finish updating code, press F5 to run, the final UI is below:

1555774408266

The code is also already pushed to github:

.netCore-WPF_Designer .

You can clone it directly to have a look.

If you want to see a preview of your WPF XAML with Visual Studio 2019 just try out this extension https://marketplace.visualstudio.com/items?itemName=GiessweinApps.FAMLDesigner .

It supports .NET Core and .NET Framework: 预览

I removed the project files and recreated the project to make it work for me. New -> Project From Existing Code...

Although the official WPF GitHub Repository states that:

Note: The WPF visual designer is not yet available and will be part of a Visual Studio 2019 update.

There is this example video in which the visual designer is being used.

Hope it helps!

Microsoft Visual Studio Professional 2019 版本 16.7.2 开箱即用。

Just found that the instruction to open the file with the WinForms designer is present in the file .csproj.user.

<ItemGroup>
    <Compile Update="Form1.cs">
        <SubType>Form</SubType>
    </Compile>
</ItemGroup>

So, if like me, you have .gitignored this file and have done a git clean, you can't open the file with the Designer anymore.

If the Forms designer is missing. Try adding another Windows Form to your project. Once I did this, the Designer for my other Windows Form appeared.

If you are using Visual Studio 2019, make sure to get the latest update which includes the Windows Form Designer. Some of the earlier version do not include it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM