dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.28k stars 956 forks source link

WinForms Designer Support #147

Closed bobasaurus closed 4 years ago

bobasaurus commented 5 years ago

In Visual Studio 2019 Preview 1, there is no project template for generating .NET Core 3 WinForms projects. If I instead run on the command line:

dotnet new winforms -o winforms_core_test

a project is created successfully. I can then open the generated .csproj file in VS2019. But the WinForms designer doesn't seem to be available. Typically the right-click context menu shows "view designer", but it is missing, and shift+f7 after opening the code doesn't work either:

missing designer

On a standard WinForms project, the Designer.cs file is typically shown as a child of the form source code, and the designer is available on the context menu:

what it should look like

Is there any way to use the designer with .NET Core 3, or is it planned in the future?

dotMorten commented 5 years ago

That limitation is already documented on the landing page : https://github.com/dotnet/winforms/blob/master/README.md

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

richlander commented 5 years ago

It is also documented in today's blog post: https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-3-preview-1-and-open-sourcing-windows-desktop-frameworks/

bobasaurus commented 5 years ago

Ah, I must have missed that. Thanks, looking forward to future support.

zsd4yr commented 5 years ago

Going to keep this issue open so that people can track anything on-going here

Tanya-Solyanik commented 5 years ago

Workaround to use the designer is to create two apps:

Steps:

  1. Using developer command prompt, create a .NET Core WinForms app. This app will be used to run the WinForms application. Steps: Open developer command prompt and run following command: "C:\Program Files\dotnet\dotnet.exe" new winforms -o <>

  2. From Visual Studio, create a new Windows Forms App (.NET Framework) project. This project will be used to design the WinForms. Steps: a. Go to File -> New -> Project b. Select Visual C# -> Windows Forms App (.NET Framework) c. From solution explorer, select Form1.cs and all the sub files under this node -> right click -> Delete d. Select project -> right click -> Add Existing Item e. Browse to the Form1.cs file in the .NET Core WinForms app folder which would have got created in Step 1 f. In the Add Existing Item dialog, select Form1.cs file and change Add to 'Add As Link' option image

    g. Build the project. There might be a build error to add a using statement for the namespace having Form1. Add that and build the project
    h. Run the application and Form1 should show up
    i. To display Form1 in designer, right click on Form1.cs -> Open with -> select CSharp Form Editor (Default) -> Ok

    image Now the Net Framework app can be used to design the WinForms and .NET Core app can be used to run the application.

Workaround provided by @Shyam-Gupta

KlausLoeffelmann commented 5 years ago

I am not sure if this will work properly, since the resulting files are not nested/missing the .designer.cs file in the classic framework project. The designer would pick this up all right, but would on CodeDom serialization create InitializeComponent in the main form class, thus projects ending up with two InitializeComponent methods and wouldn't really compile?

(Correct me, if I am missing something here!).

I think what's need to be done here is adding links to all three files, and then nest them in the Classic Framework Project in addition, so changes from the Visual Designer would actually go into the form.designer.cs.

jnm2 commented 5 years ago

Duplicate of https://github.com/dotnet/project-system/issues/2963?

zsd4yr commented 5 years ago

@jnm2 this issue tracks only Winforms while https://github.com/dotnet/project-system/issues/2963 is tracking progress for Winforms and WPF. I don’t think either has to be duped, but feel free to relate the two :)

maikebing commented 5 years ago

Failed to load file or assembly "System.ComponentModel.TypeConverter, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a "Or one of its dependencies. The system could not find the specified file.

wjk commented 5 years ago

I've figured out how to workaround the assembly-load failure identified by @maikebing. What you need to do is to multitarget your .NET Core (SDK-style csproj) project to multitarget .NET Framework as well as .NET Core, ensuring that the .NET Framework TFM is first in the list. This will cause VS to attempt to resolve the designer types using .NET Framework before it tries (and fails) to do so using .NET Core.

Only problem I have identified is, when I try to debug it, it now debugs only the .NET Framework version. I am not certain how to instruct the VS debugger to target the .NET Core version, while not breaking the designer. In the meanwhile, we can run the .NET Core EXE from the command line and do Debug > Attach to Process.

In short: The Windows Forms designer now works with Visual Studio 2019! 🎉 🎉 🎉

AraHaan commented 5 years ago

@wjk you mean Visual Studio 2019?

wjk commented 5 years ago

@AraHaan Yes, sorry!

AraHaan commented 5 years ago

@wjk do you also know if the visual studio forms designer is now open source too and if I can force a local build of roslyn and the forms designers and use them in the latest Visual Studio to experimemt on some changes:

like making the forms designer use roslyn to somehow display the form and load resources instead of background compile files and then use reflection.

Useful if you want to manually cause resources to load from a native resources file or something (.rc / .res) in which case could possibly demand some p/invoke some windows apis. I had a problem similarly when I did not want to add an extra 3 MB onto the image as the application icon is large (3 MB) itself and figured I could have it load it from the native resource section instead of loading it from a embedded resx to save some space.

wjk commented 5 years ago

@AraHaan Unfortunately, the WinForms designer is not open-source, nor is it compatible with .NET Core. It must remain on .NET Framework, because it is used with Visual Studio, which cannot be ported to Core for backwards-compatibility reasons.

Furthermore, while the designer code can be embedded within a third-party Framework application (see the types in the System.ComponentModel.Design and System.Windows.Forms.Design namespaces), there is no public code sample (that I can find) that shows how to host the full Windows Forms designer in your own application. However, none of this code is usable on Core (most of it is just missing, while other parts throw NotImplementedExceptions "by design"). I added a comment in #221 questioning this decision, but there is no response from Microsoft yet. I wouldn't get your hopes up. Sorry about that!

dreddy-work commented 5 years ago

@wjk , the reason behind the type that are throwing "NotImplemented" exception in core was based on the usage metrics we received, at the time of porting, and possible scenarios where they were used. Our understanding was most of these types used only at the design time and may need only for compile purpose at run-time. For the types that were completely missing, Metrics we collected showed no usage. However, we are looking at the various requests and git hub issues raised, We will be working on them individually. Expect some of these types available in the next Preview releases.

Zonciu commented 5 years ago

Before VS2019 supports .Net Core WinForms & WPF Designer, we still be able to use this trick. If Designer reports crash exception, just rebuild your project, or rebuild your project and then restart VS2019.

============================================================

I found an easy way to make VS2019 support net core 3 WinForm & WPF: netcore3-winform-designer netcore3-wpf-designer

Design in .NET Framework target, publish in .NET Core 3.

WinForm

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>
    <UseWindowsForms>true</UseWindowsForms>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Deployment" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
    <!-- add From files to here to enable designer -->
    <Compile Update="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
  </ItemGroup>
</Project>

WPF

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
    <!-- add files to here to enable designer -->
    <Compile Update="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
    <Compile Update="MainWindow.xaml.cs">
      <DependentUpon>MainWindow.xaml</DependentUpon>
      <SubType>Code</SubType>
    </Compile>
  </ItemGroup>
</Project>
dotMorten commented 5 years ago

I found a easy way to make VS2019 preview support net core 3 winform

Technically you're not getting design time support for .NET Core, but for .NET Framework, but still a convenient and clever trick! Thanks for sharing.

I wonder if we could just do this trick for design time builds, rather than having to multi-target and compile twice? I was hacking around a bit by flipping on the $(DesignTime) flag, but it seems to be tripping VS up even more. I sort of got it working sometimes, only for it to fail shortly after.

Also this trick works in WPF !

Zonciu commented 5 years ago

Technically you're not getting design time support for .NET Core, but for .NET Framework

It is a temporary solution before official .NET Core Designer releasing, thanks for the new project system's compatibility!

Also this trick works in WPF !

Do this trick in WPF project can make Blend for VS2019 works well!

jnm2 commented 5 years ago

Blender is not the same product as Blend.

JohnyL commented 5 years ago

It's Visual Studio 2019 16.0.2 now - and STILL no support for both WinForms and XAML designers!

KN4CK3R commented 5 years ago

This feature isn't even released. So there is STILL no support.

jnm2 commented 5 years ago

@JohnyL The roadmap clearly states that designer support will come much later than the release of VS2019.

by8 commented 5 years ago

@Zonciu In that way, the project is not pure .net core project any more. When you open the property in VS2019, the target framework is blank. It does not make sense.

Zonciu commented 5 years ago

@by8 Usually I modify the target framework by editing the csproj file. In order to add multiple target frameworks it requires to change <TargetFramework> to <TargetFrameworks>, however VS only shows the <TargetFramework> config, that's why when you add multiple target frameworks, The Target Framework selector will be blank and uneditable.

In that way, the project is not pure .net core project any more.

I only use this trick to experience .NET Core WinForm/WPF, before official support, it shouldn't be used in production environment. And it is very easy to cleanup, it doesn't pollute your code(unless .NET Framework's control has different code with .NET Core),you just need to remove the NET471 config, or create another pure .NET Core csproj file to replace it.

chucklu commented 5 years ago

@Zonciu Thanks for your temporary solution.

AraHaan commented 5 years ago

hmm if only VS actually edited that option and actually had a list box (for .net core and .net standard projects) to select which ones they want from items in a list box (of supported frameworks on the latest version of the .net core sdk they have installed on their device).

That way if they select 1, it will be <TargetFramework> if they select multiple, it will be <TargetFrameworks> and probably easy to implement. In fact if that page was open source I would do it myself, test it, then PR it back to Microsoft.

That way people do not have to open the project file, add multiple frameworks, then forget to add the s to <TargetFramework> to make it <TargetFrameworks> and then they wonder why it wont load.

davidwengier commented 5 years ago

In fact if that page was open source I would do it myself, test it, then PR it back to Microsoft.

The property pages are indeed open source, and there is an open issue for better support for the multi-target scenario here: https://github.com/dotnet/project-system/issues/800

AraHaan commented 5 years ago

Oh nice, the page (the one that shows up in the IDE) happen to have a designer option so I can PR a fix for it then?

Also where is the page located?

giggio commented 4 years ago

Summarizing to anyone coming across this in the future. All you have to do to get the designer to show is this on your csproj:

<TargetFrameworks>net48;netcoreapp3.0</TargetFrameworks>

A lower version of .NET Framework should work too, like net472.

RussKie commented 4 years ago

Correct, thank you. The information has been captured in https://github.com/dotnet/winforms/blob/master/Documentation/winforms-designer.md.

maikebing commented 4 years ago

Does anyone know when the designer plans to release it?

RussKie commented 4 years ago

A preview of the designer will be released as part of .NET 3.0 release in September 2019.

chatilaah commented 4 years ago

If you want to make the designer work before Microsoft announces the GA of .NET Core 3.0, visit this link: https://www.codeproject.com/Articles/5163923/Use-Windows-Forms-Designer-on-NET-Core-3-0-Preview

RussKie commented 4 years ago

Thank you. However the offered solution is more complex than our guide.

Also the file copying logic could be expressed in a form of MSBuild targets, which would make it both cleaner and more maintainable.

PeterSawyer commented 4 years ago

Having a separate Designer project works ok, but requires all files in the project to be added as links and when using a package like Microsoft.EntityFrameworkCore.Sqlite then Designer is unable to compile as there is no equiv namespace in .NET FW 472.

So have to make Design changes, ignore errors, then Unload project before building.
Is this the expected behavior in the interim or am I missing something?

Can I stop Designer proj from causing errors in my solution? Looking forward to September. (also tried the TargetFrameworks but crashes VS)

chatilaah commented 4 years ago

The older .NET Framework WinForms project is supposed to act as a reflector (design wise). You don’t need to reference anything, any UI changes you wish to make will be directly applied to the .NET Core project.

PeterSawyer commented 4 years ago

You don’t need to reference anything,

Thanks - I am so used to doing a CTRL+SHIFT+B to compile the solution. Might be best I keep the older (design) project in a separate solution, thus VS window.

AraHaan commented 4 years ago

@PeterSawyer or just look at the updated options; you can also list the framework that you use on the designer project on your core project; then delete the designer one; and it would still do the same. Note: that framework MUST be listed first.

PeterSawyer commented 4 years ago

@AraHaan tried that by changing <TargetFramework>netcoreapp3.0</TargetFramework> to <TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks> and targeted 472 for the form as per the following, however, I can open a form and edit in the designer but whenever I try to save the form i get an error saying "Microsoft Visual Studio - Specified cast is not valid."

  <ItemGroup Condition="'$(TargetFramework)' == 'net472'">
    <Compile Update="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Update="Form1.Designer.cs">
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
  </ItemGroup>

My current work around is to create a stub mock class in the Designer project to reference instead of linking the real class in the .netcore app which uses EntityFrameworkCore.

    public class VehicleDataService {
        public void Post(Vehicle vehicle) {
            throw new NotImplementedException();
        }

        public Vehicle GetByCode(string vehicleCode) {
            throw new NotImplementedException();
        }
    }
kirsan31 commented 4 years ago

@PeterSawyer

tried that by changing netcoreapp3.0 to net472;netcoreapp3.0 and targeted 472 for the form as per the following, however, I can open a form and edit in the designer but whenever I try to save the form i get an error saying "Microsoft Visual Studio - Specified cast is not valid."

Same here. I've done it as in Option 1. And got Specified cast is not valid every time.

chatilaah commented 4 years ago

If you want to make the designer work before Microsoft announces the GA of .NET Core 3.0, visit this link: https://www.codeproject.com/Articles/5163923/Use-Windows-Forms-Designer-on-NET-Core-3-0-Preview

In case anyone is looking to avoid reading the article provided, I've recorded a video on how to use WinForms Designer in .NET Core 3.0 Preview. https://www.youtube.com/watch?v=n9rqu_L7JUY

dbeavon commented 4 years ago

Any word on the new core designer preview? I didn't see any winforms speakers in the lineup at .net conf and I'm wondering if that means there won't be a preview yet...

filipnavara commented 4 years ago

@dbeavon "Modernizing .NET Desktop Applications with .NET Core - Olia Gavrysh
11:30 (PDT) | 18:30 (UTC)"

merriemcgaw commented 4 years ago

We have our first Designer Preview available today! This is an early sneak peak of the work we've been doing. Check out designer documentation for links to the release link, known issues and Release Notes.

Please try this out! It requires 16.3 Preview 4 or later and the 3.0 Core SDK. You can provide your feedback via the VS Feedback tool. We're planning on an ongoing monthly release cadence once 3.1 ships so that you can see what we're adding and improving each month.

[EDIT] fixed the link

jnm2 commented 4 years ago

https://github.com/dotnet/winforms/blob/master/Documentation/designer-releases/readme.md? The relative link doesn't work from this page.

fatihyildizhan commented 4 years ago

<TargetFrameworks>net48;netcoreapp3.0</TargetFrameworks> didn't work for me but <TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks> works fine for both WinForms and WPF with Visual Studio 16.3.0.

RussKie commented 4 years ago

<TargetFrameworks>net48;netcoreapp3.0</TargetFrameworks> didn't work for me

Do you have .NET 4.8 installed?

merriemcgaw commented 4 years ago

And do you have the targeting pack installed for .NET 4.8?

KaneLeung commented 4 years ago

netcore 3.0 designer preview you can use this vsix TIM截图20190925103338

RussKie commented 4 years ago

I'm going to close this since we have released a Windows Forms Designer for .NET Core; more information here - https://github.com/dotnet/winforms/tree/master/Documentation/designer-releases. The current Designer is still in a preview state and supports the basic set of control. The team is working around the clock to bridge the remaining gap.