MAUI Mac VS - Warning CS8785: Generator 'CodeBehindGenerator' failed - CS0103 Initialize component does not exist

Leo Wagner de Souza 696 Reputation points
2023-08-03T16:40:23.3066667+00:00

I have a MAUI app, that was migrated from Xamarin Forms some time ago.

The app builds for iOS and Android from the command line (Jenkins build).

The app builds for Android from a Windows machine.

Now, I'm using a MacBook Pro with VS for Mac as development environment.

However, when I try to build from Mac VS, I get the error:

CS0103: InitializeComponent does not exist in the current context.

I know that the MAUI InitializeComponent method is generated by MAUI source generator, so I looked in the logs, and found:

/Users/lsouza/Documents/Dev/AHT/windows/Aht.SMT.Mobile/Aht.SMT.Mobile.Maui/CSC: Warning CS8785: Generator 'CodeBehindGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'NullReferenceException' with message 'Object reference not set to an instance of an object.' (CS8785) (Aht.SMT.Mobile.Maui)

A blank MAUI solution builds perfectly in Mac VS, but not this one.

Any ideas on how to solve this NullReferenceException on CodeBehindGenerator?

Developer technologies | .NET | .NET MAUI
Developer technologies | Visual Studio | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Leo Wagner de Souza 696 Reputation points
    2023-08-04T20:56:54.0766667+00:00

    The problem is solved. Another developer added a dependency to the project, but stored the source code in his personal Git account.

    When I pulled the code from the company Git, this dependency was left out.

    So, when the CodeBehindGenerator tried to parse the page XAML, it found this tags, which it could not resolve, throwing the null exception.

    The strange thing is that it did not generate the InitializeComponent for any page, and not just to the page that used the broken XAML tag.

    Fix the broken dependency fixed the CodeBehindGenerator and the InitializeComponent.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.