9

I've created a WPF application using the new CSPROJ format in Visual Studio 2017.

By

I can successfully build and run the application. However, I have an issue in that the code editor does not recognize any controls placed in the XAML, so I get erroneous errors and no intellisense in the editor.

Erroneous errors in the code editor

Steps to reproduce

  1. Launch VS 2017
  2. Create a new "WPF App (.NET Framework)" C# project
  3. Edit the csproj file to look like:

Project file:

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
  <PropertyGroup>
    <LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
    <TargetFramework>net45</TargetFramework>
    <ProjectGuid>{030D04DA-D603-4D4C-95F7-B6F725A6829E}</ProjectGuid>
  </PropertyGroup>
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
  </PropertyGroup>
  <PropertyGroup>
    <StartupObject />
  </PropertyGroup>
  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    <Page Include="MainWindow.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:Compile</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System.Xaml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
</Project>
  1. Add a button called "button1" to MainWindow.xaml as below:

MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="button1" Width="100" Height="50">Click me!</Button>
    </Grid>
</Window>
  1. Try and set the button caption in code in "MainWindow.xaml.cs".

You will see that intellisense does not recognise that the button is defined: Erroneous errors in the code editor

However, the project builds and runs successfully (note you will need to run the executable manually - F5 doesn't work...)

RB.
  • 36,301
  • 12
  • 91
  • 131
  • Did you find any solution to this? – Ivan Zlatanov Aug 30 '17 at 07:49
  • Currently I don't believe there is a solution - we are sticking with the new project format as it has some other big advantages for us (conditional nuget packages is the killer for our scenario, but transitive dependencies are super nice too), but this is definitely a PITA for us :( – RB. Aug 30 '17 at 07:50
  • This appears to be fixed in Visual Studio 2017 v15.3.3 - at last! – RB. Sep 12 '17 at 09:39

3 Answers3

2

A slight improvement on your previous answer is to include the .g.cs files, but mark them as not visible so they do not show in the solution. You will then also need to mark the BaseIntermediateOutputPath as not visible otherwise it shows up as an empty folder.

This gives the same behaviour but looks tidier as you're not seeing the obj folder in the solution explorer.

<ItemGroup>
  <ApplicationDefinition Include="App.xaml">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
  </ApplicationDefinition>
  <Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" Exclude="App.xaml" />
  <Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
  <Compile Include="$(IntermediateOutputPath)*.g.cs" Visible="false" />
  <None Include="$(BaseIntermediateOutputPath)" Visible="false" />
</ItemGroup>
Andy
  • 541
  • 6
  • 9
  • Further investigation shows that this does not work consistently. Generally, when you do a build, intellisense stops working. – RB. Jun 23 '17 at 15:36
  • This appears to work with Visual Studio 2017 v15.3.3 now, so marking it as the answer. – RB. Sep 12 '17 at 09:40
  • Note that I had to tweak the penultimate line to include child folders `` – RB. Sep 12 '17 at 10:38
  • Oh, and projects won't load if you have this line: ``. Still - it's definitely an improvement :) – RB. Sep 12 '17 at 13:42
1

One slightly hacky solution is to edit the project file to add the generated classes. Unfortunately, I can't work out how to make the file be DependentUpon the XAML file - I think that only works for files in the same folder.

In addition, you need to use MSBuild:UpdateDesignTimeXaml instead of MSBuild:Compile, as described here

Finally, it appears you have to explicitly name every "Include" in the "Page" element - you can't use wildcards.

<Project>
  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </ApplicationDefinition>
    <Page Include="MainWindow.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
    <!-- ADD THIS LINE HERE TO INCLUDE THE GENERATED PARTIAL CLASSES -->
    <!-- ENSURE YOU ARE USING THE g.cs AND NOT g.i.cs FILES 
         OR YOU WILL GET A BUILD FAILURE -->
    <Compile Include="$(IntermediateOutputPath)**\*.g.cs" />
  </ItemGroup>
RB.
  • 36,301
  • 12
  • 91
  • 131
  • Further investigation shows that this does not work consistently. Generally, when you do a build, intellisense stops working. – RB. Jun 23 '17 at 15:36
1

I found that using this like to define the Sdk resolved it for me:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="MSBuild.Sdk.Extras">
  <PropertyGroup>
....

instead of

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

(This is in 15.9.15 version of VS2017)

ScottFoster1000
  • 577
  • 1
  • 10
  • 25