Is it OK to enable both Winforms and WPF for a .NET 6.0 application?

Viewed 374

As shown here and here (both on the same Microsoft page) a .NET 6.0 application can target Winforms and WPF. But will that cause conflicts at some point? What isn't clear is whether both can be targeted in the same project.

So, is it OK to target both together or will alters the MSBuild pipeline to correctly process a Windows Forms project conflict with alters the MSBuild pipeline to correctly process a WPF project?

Here's how it looks in Visual Studio in the project's properties tab:

enter image description here

The reason I'm asking is because I want to use certain classes from Winforms in a WPF application.

2 Answers

Yes, it's OK and there are valid scenarios which you may want to add both frameworks. It's already mentioned in the documentations, you can add both, if necessary:

  • Add a UI framework property (or both, if necessary):

And also the example shows the same:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>

    <UseWPF>true</UseWPF>
    <!-- and/or -->
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

Yes, this works just fine. You can have an application use both WinForms and WPF, even intermixed. There are even controls to host a WPF control inside a Winforms window and WinForms controls inside a WPF window (see e.g. here).

There are a some limits when doing this extensively (WPF hosts WinForms control, hosts WPF control hosts... etc may not work) but I've been working on a GUI that has everything intermixed (for legacy reasons) and it is generally working great.

Related