Bug 55593 - The name 'InitializeComponent' does not exist in the current context with .NETStandard/new csproj format portable library
Summary: The name 'InitializeComponent' does not exist in the current context with .NE...
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: .NET Core support ()
Version: 7.0 (VSforMac)
Hardware: PC Mac OS
: Normal normal
Target Milestone: 15.2
Assignee: Matt Ward
Depends on:
Reported: 2017-04-25 20:38 UTC by Kyle White
Modified: 2017-08-02 19:26 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Kyle White 2017-04-25 20:38:39 UTC
I can reproduce this with the following solution: https://github.com/onovotny/SampleXamFormsWithNetStandard

The portable project in there is a .NETStandard library using the new (sdk style?) csproj format.

I do not get this error with PCL library (ie. File > New Project).

Screenshot: https://screencast.com/t/RD7ustds
About: https://gist.github.com/kdubau/401a4a42bd922ca4ee12d9adbab9a510
Comment 1 Matt Ward 2017-04-26 13:49:58 UTC
OK so some initial investigation below:

With a .xaml file the InitializeComponent method is defined in a generated file (e.g. obj/Debug/RootNamespace.MainPage.xaml.g.cs).

With a normal C# PCL project, with the Xamarin.Forms NuGet package installed, the generated .xaml.g.cs files are added to the list of source files in Project.OnGetSourceFiles so they are available for the type system and code completion.

At a lower level, the .xaml.g.cs files themselves are determined by running the targets that CoreCompileDependsOn defines. This includes the XamlG target provided by the Xamarin.Forms MSBuild targets. The XamlG targets will add the .xaml.g.cs files.

With a .NET Core project the CoreCompileDependsOn is currently not defined. It returns null so no project files are added here so the .xaml.g.cs files are not being made available to the type system. This results in the error in the text editor. Building the project works fine.

I suspect the problem is that the generated NuGet files (e.g. obj/ProjectName.nuget.g.targets) are not being used by the project model. These generated NuGet files have imports for the Xamarin.Forms MSBuild targets. The Xamarin.Forms MSBuild targets modify CoreCompileDependsOn so it depends on the XamlG target.
Comment 2 Lluis Sanchez 2017-04-26 16:05:48 UTC
How are targets like ProjectName.nuget.g.targets imported into the main project by MSBuild?
Comment 3 Matt Ward 2017-04-26 17:18:13 UTC
Not sure. There are some NuGet.targets that get included by MSBuild Microsoft.NuGet.targets and Microsoft.NuGet.ImportAfter.targets but I do not see anything explicitly importing the nuget.g.targets or nuget.g.props files. Asked Ankit about this.

There is a fix for this on the d15-2-dotnetcore-imports-from-generated-nuget-targets branch. Still need to add a test for this.
Comment 4 Matt Ward 2017-05-02 10:08:19 UTC
Pull request for d15-2:

Comment 5 xamarin-release-manager 2017-05-02 10:17:11 UTC
Fixed in version (d15-2)

Author: Matt Ward
Commit: 2bc0c06ca2da61c001a8d505dc96bc03fd75aa8e (mono/monodevelop)
Comment 6 Neha Kharbade 2017-05-05 07:06:05 UTC

Verified the bug on the latest build :-
Visual Studio for Mac Version Preview 10 (7.0 build 3111)
Xamarin.iOS Version:
Xamarin.Android Version:

Build Information :-

Screencast link :-

Hence marking the bug as verified.