Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet build fails for projects containing UserControl (InitializeComponent does not exist in the current context)

Tags:

I have a project with the .csproj defined using the .NET Core format. The project contains user control files consisting of a .xaml file and an associated .cs file. Previously I got a compilation error on those user controls until I added the following content to the .csproj file:

  <ItemGroup>
    <Page Include="**\*.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
  </ItemGroup>

At that point the solution began to compile within Visual Studio. However, when I attempt to build from the command line using dotnet build, the build fails with the error I had previously seen in Visual Studio: The name 'InitializeComponent' does not exist in the current context.

Any ideas why the solution builds in VS2017 but not using dotnet build, or how I go about fixing this?

EDIT: .csproj content without PackageReferences

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <LanguageTargets>$(MSBuildToolsPath)\Microsoft.CSharp.targets</LanguageTargets>
    <TargetFramework>net45</TargetFramework>
    <IsPackable>true</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <Page Include="**\*.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </Page>
    <Compile Update="**\*.xaml.cs" SubType="Code" DependentUpon="%(Filename)" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xaml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
</Project>
like image 709
ket Avatar asked Apr 10 '18 16:04

ket


1 Answers

Dotnet build fails for projects containing UserControl (InitializeComponent does not exist in the current context)

At this moment, I am afraid you have to use the MSBuild cli instead of using dotnet build. That because XAML files are not supported by the dotnet cli at this moment. MS team is working hard to solve this problem.

Although, The new sdk csproj (net461) works after compiling and deploying, but Visual Studio can't parse .xaml files during development as it expects .NET core views.

So, to resolve this issue, you can use the MSBuild cli instead of dotnet build.

See XAML files are not supported for more details.

like image 108
Leo Liu-MSFT Avatar answered Sep 28 '22 05:09

Leo Liu-MSFT