dotnet / project-system

The .NET Project System for Visual Studio
MIT License
969 stars 387 forks source link

VS2019 not handling well WPF project with SDK style csproj (without .NET Core 3.0 SDK) #4686

Closed petroemil closed 4 years ago

petroemil commented 5 years ago

Visual Studio Version: 2019

Summary: Before the .NET Core 3.0 SDK (that is still not out of Preview at the time of raising this issue), there was a "hack" to make it possible to use SDK style project files with WPF projects.

<Project Sdk="Sunburst.NET.Sdk.WPF/1.0.47">

With Visual Studio 2017 this worked perfectly well, but in Visual Studio 2019 these projects seems to be broken, displaying only XAML files in the Solution Explorer and not picked up by build either.

Steps to Reproduce:

  1. Clone the sample project from my repro repo
  2. Try to open and build/run it in Visual Studio 2017
  3. Try to open and build/run it in Visual Studio 2019

Expected Behavior: Work just like it used to in VS2017

Actual Behavior: Broken - as explained above

User Impact: Can't work on these WPF projects in VS2019

clairernovotny commented 5 years ago

Sunburst.NET.Sdk.WPF appears to be a partial fork of my MSBuild.Sdk.Extras SDK. It hasn't been updated in a year and the repo is marked as archived on GitHub: https://github.com/SunburstApps/MSBuildSdks

Can you try using MSBuild.Sdk.Extras/2.0.24 and see if that solves your problem?

petroemil commented 5 years ago

Using MSBuild.Sdk.Extras/2.0.24 raises other problems.

  1. When trying to build the project, it complains about not finding an entry point - error CS5001: Program does not contain a static 'Main' method suitable for an entry point
  2. Even if I manually add an entry point and call App.Run, the XAML files are still not compiled properly and they keep complaining about missing InitializeComponent
  3. Also, while it all looks good in VS2019, in VS2017 the association between xaml and xaml.cs files is lost and they are just listed out flat in the Solution Explorer - though I'd be fine with this issue, it's just aesthetics
clairernovotny commented 5 years ago

For the Extras, have you added a property UseWpf set to true?

petroemil commented 5 years ago

I have, but doesn't seem to make any difference.

I really don't want to waste anybody's time... is it supposed to work (with the SDK Extras) or should I just wait for the .NET Core 3.0 SDK to come out of Preview and then I can use this?

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
davkean commented 5 years ago

We have no official support for WPF outside of what's coming with .NET Core 3.0; there's non-trivial amount of bugs/work (such as real code generator support) that will only work in 16.1 onwards.

For the reason that project is broken in VS 2019, is because of:

System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\16.0\Bin\Microsoft.CSharp.targets'.'

That path in Dev16 is; C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.CSharp.targets'. The SDK needs to be fixed.

davkean commented 5 years ago

We shouldn't be letting you open this project due some critical missing components, trying to figure out why we do.

ForNeVeR commented 5 years ago

@petroemil (and anyone else who encountered the same Sunburst.NET.Sdk.WPF issue on VS2019), for your information, I've ended up with forking the Sunburst.NET.Sdk.WPF and publishing the fixed version to NuGet. Please find the fixed package here, and the sources here.

zspitz commented 4 years ago

I've had success by creating a shared project and storing the XAML files along with their code behind in the shared project. Other code files can go in the regular (SDK-format) project folder.

davkean commented 4 years ago

We added support for WPF projects in 16.3, closing this out.

zspitz commented 4 years ago

@davkean

We added support for WPF projects in 16.3, closing this out.

To clarify, do you mean support for .NET Core WPF projects? Or do you mean WPF .NET Framework projects using the SDK-style csproj?

davkean commented 4 years ago

We also added support for consuming WPF from a .NET Framework SDK-style project.

Skyppid commented 4 years ago

@davkean I got to ask how that's supposed to be working. We created .Net Standard projects which were initially not inteded to contain WPF. Yet we had to add WPF-related classes (UserControls for example) and thus changed the target framework to "net472". Currently I tried both "Microsoft.NET.Sdk" and "Microsoft.NET.Sdk.WindowsDesktop". Yet VisualStudio does show ".xaml" and ".xaml.cs" files separately, I cannot switch to code via F7 in designer and I have no templates available for WPF (not a drastic issue, yet annoying).

I simply can't find any resource on how to set up the project file so this is all working. Sadly there's lots of intransparancy and a lack of resources.

I am not yet permitted to upgrade to .Net Core 3 as we're having two open not yet finished projects and my team leader doesn't want to introduce any unnecessary issues/bugs right now.

Thank you for any help!

zspitz commented 4 years ago

@Skyppid Generally, the following works for me:

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

  <PropertyGroup>
    <TargetFrameworks>net472</TargetFrameworks>
    <RootNamespace>MyProjectName</RootNamespace>
    <AssemblyName>MyProjectName</AssemblyName>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

For me, Solution Explorer groups the code-behind files under the XAML files, and provides proper Intellisense when opening the XAML. I also get WPF templates for Add new item....

There still appears to be a bug when using XAML in a shared project in VS Community currently under investigation. If this applies to you, you could try one of the listed workarounds -- either

  1. install the UWP workload, or
  2. open the XAML file using File -> Open instead of from Solution Explorer, or
  3. right-click and Open With ... -> Source Code (Text) Editor
Skyppid commented 4 years ago

Thanks @zspitz I indeed overlooked the "UseWPF". But nonetheless (even after a restart) the Solution Explorer does not merge designer and code files together. Everything else works after a few hiccups.

EDIT: Tried removing all .xaml related entries in the project file, excluded them and re-added them (both, only .cs, only .xaml) and it just doesn't get it. It always shows them as seperate files. They're named the same of course.

zspitz commented 4 years ago

@Skyppid

  1. What version of VS are you using?
  2. If you open the XAML file using File -> Open or *Open With..., do you get full Intellisense? At that point, can you toggle between the XAML and the code-behind using F7?
Skyppid commented 4 years ago

@zspitz I use the latest 2019 (16.6) version. If I do that, I cannot switch to code either.

EDIT: I found the culprit. The files were named .cs and not .xaml.cs for some reason (I used to copy them because the templates did not work - not sure why the .xaml. went missing.

Sorry for the inconvenience!