Gregori Schuster
Gregori Schuster

Reputation: 43

Error CS0103 The name 'InitializeComponent' does not exist in the current context in project upgrade from xamarin.forms to .NET MAUI

I am in the process of updating my Xamarin.Forms project to .NET MAUI and am encountering the error

Error CS0103: The name 'InitializeComponent' does not exist in the current context.

The problem appears to be that Maui SourceGen was unable to generate the xaml.sg.cs file for the xaml.cs file.

I have noticed that topic MAUI project, cannot add a new Page and topic .NET MAUI: The name 'InitializeComponent' does not exist in the current context discuss this subject, but following the responses provided there, none have resolved my issue, which I find peculiar.

I created a new Maui content page in the following manner. enter image description here

The file xaml is structured as follows.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Ren9ve.ERP.Mobile.NewPage1"
             Title="NewPage1">
    <VerticalStackLayout>
        <Label 
            Text="Welcome to .NET MAUI!"
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

And the xaml.cs file.

using Microsoft.Maui.Controls;

namespace Ren9ve.ERP.Mobile
{
    public partial class NewPage1 : ContentPage
    {
        public NewPage1()
        {
            //Here is where the reported error occurs.
            InitializeComponent();
        }
    }
}

In the .csproj file, it is set up as follows.

  <ItemGroup>
    <MauiXaml Update="NewPage1.xaml">
      <Generator>MSBuild:Compile</Generator>
    </MauiXaml>
  </ItemGroup>

If I create a new Maui project in the same solution and create an identical file, it works normally. The error only occurs in the project that I am updating to Maui. The error occurs in all XAML files, but for testing purposes, I commented out all the CS0103 errors and left only the one from the new file.

I'm using VS2022 17.8.4

  1. I have set the build action for the C# file to C# Compiler
  2. I have cleaned and rebuilt my project several times and tried deleting the bin and obj folders, but nothing seems to work
  3. I attempted to test on a different machine.
  4. I compared the csproj with one from a new Maui project but didn't find anything that could cause the issue.
  5. I tried implementing solution MAUI project, cannot add a new Page but it also did not work.

Upvotes: 1

Views: 1581

Answers (1)

JansenSensei
JansenSensei

Reputation: 61

I am new to working in MAUI an as such I ran into this error a Lot.... so much so that I found the answer to this problem.... but you are not going to like it. :(

95% of the time the reason why the code behind throws me that error was because there was SOMETHING wrong in the XAML file. No matter what it is, if there is an error in there then I get that error in the .cs file. Looking through the xaml for typos or just plain poor xlml fixes this issue 95% of the time.

The other 5% of the time it just pure dumb luck. It's exactly the same problem only the error happens on two to 4 different scripts when the actual typo happened in a completely unrelated script to all of them. I just so happened to have worked on File5.xaml last so when I got all those errors in files 1 .. 4 I just fixed the typos in File5 and voila, the bugs in the other 4 files just went away...

So yeah, the problem is definitely a bug in your XAML but only a 95% chance that the bug is where VS tells you it is...

Good luck

Upvotes: 0

Related Questions