Bug 39827 - "The name 'InitializeComponent' does not exist" when using XAML files in a shared project that is referenced from a UWP project
Summary: "The name 'InitializeComponent' does not exist" when using XAML files in a sh...
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows (show other bugs)
Version: 2.1.0
Hardware: PC Windows
: --- major
Target Milestone: UWP
Assignee: Bugzilla
URL:
: 36935 (view as bug list)
Depends on: 32988
Blocks:
  Show dependency tree
 
Reported: 2016-03-22 04:32 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2016-04-09 02:56 UTC (History)
4 users (show)

Tags: ac
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:
Status:
VERIFIED FIXED

Description Brendan Zagaeski (Xamarin Team, assistant) 2016-03-22 04:32:40 UTC
"'InitializeComponent' does not exist" because no `.g.cs` files are generated during the build for Xamarin.Forms XAML files in a shared project when referenced from a UWP project that itself includes any XAML files




The fix for this might be similar to the old fix for Bug 29501? That old bug (about Windows Store Windows 8.1 projects rather than UWP projects) had a very similar behavior until it was fixed.




## Steps to replicate

1. Create a new "Visual C# -> Windows -> Universal -> Blank App (Universal Windows)" in VS 2015 Update 1 on Windows 10. (For my tests I used ".NET Framework 4.5.2" in the target framework drop-down.)

2. Install the Xamarin.Forms 2.1.0.6529 NuGet package into the project.

3. Add a new "Visual C# -> Shared Project" to the solution.

4. Add a reference to the shared project from the UWP project.

5. Add a new "Cross-Platform -> Forms Xaml Page" to the shared project.

6. Build the UWP project in the "Debug|x86" configuration.



### Variations

- You can optionally apply the "Partial Workaround" from Bug 39826, Comment 0 after step 6 and then rebuild. That partial workaround has _no effect_ in this case, suggesting that this bug has a different underlying cause compared to Bug 39826.

- You can optionally replace steps 1 - 4 with a single step of creating a new "Visual C# -> Cross-Platform -> Blank App (Xamarin.Forms Shared)" project. The results will be the same.




## Results

No `.g.cs` file is created for the Xamarin.Forms XAML page. The most visible consequence is that the build fails because `InitializeComponent()` is not defined:

> c:\source\UWPBlankApp1\SharedProject1\Page1.xaml.cs(15,4,15,23): error
> CS0103: The name 'InitializeComponent' does not exist in the current
> context


### Listing of `.g.cs` files in the `obj\` folder

> C:\source\UWPBlankApp1\UWPBlankApp1>dir /B obj\x86\Debug\*.g.cs
> App.g.cs
> MainPage.g.cs
> XamlTypeInfo.g.cs




## Additional Experiment: "interference" from the UWP XAML pages?

1. Remove the `App.xaml` and `MainPage.xaml` files from the UWP project.

2. Add a minimal `Program.cs` file in the UWP project that defines a `static void Main(string[] args){}` method.

3. Build the UWP project in the "Debug|x86" configuration.



Now the `.g.cs` file is created as expected, and the build completes successfully.

(Note: This modified project won't _run_ as expected because the `Program.cs` file is too simple. The goal of this experiment is just to demonstrate the _build_ behavior.)



### Listing of `.g.cs` files in the `obj\` folder after this new experiment

> C:\source\UWPBlankApp1\UWPBlankApp1>dir /B obj\x86\Debug\*.g.cs
> UWPBlankApp1.c_.source.UWPBlankApp1.SharedProject1.Page1.xaml.g.cs




## Additional version info (brief)

Microsoft Visual Studio Professional 2015
Version 14.0.24720.00 Update 1

Windows 10 (64-bit)
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2016-03-22 04:50:59 UTC
*** Bug 36935 has been marked as a duplicate of this bug. ***
Comment 3 Brendan Zagaeski (Xamarin Team, assistant) 2016-03-23 01:58:55 UTC
Quote from David Karlaš:

Problem is that .Net targets that generate .g.cs for UWP/WP .xaml files(XamlPreCompile) are called before Xamarin.Forms XamlG target that generates .g.cs for XF .xaml files...
And those targets(UWP/WP) depend on being able to compile whole project before they can perform conversion...

Workaround I used... I replaced:
<Target Name="XamlG" DependsOnTargets="$(XamlGDependsOn)"/>
with
<Target Name="XamlG" BeforeTargets="XamlPreCompile" DependsOnTargets="$(XamlGDependsOn)"/>

In .targets file of Xamarin.Forms the one that come inside .nuget package... So XF .xaml->.g.cs is generated before UWP .xaml->g.cs conversion targets are called and everything works as expected...
Comment 6 Brendan Zagaeski (Xamarin Team, assistant) 2016-03-23 21:27:18 UTC
## Note to the Xamarin team

It turns out that using `BeforeTargets="XamlPreCompile"` introduces some incompatibilities when building with Xamarin Studio on Mac. There is an in-progress discussion of this complication along with possible alternatives starting at Bug 32988, Comment 22.
Comment 7 Brendan Zagaeski (Xamarin Team, assistant) 2016-04-09 02:54:58 UTC
## Verification status: verified fixed on Xamarin.Forms 2.2.0.5-pre2

GOOD: Xamarin.Forms 2.2.0.5-pre2
BAD:  Xamarin.Forms 2.1.0.6529