MAUI project, cannot add a new Page

Viewed 2836

I'm trying out MAUI and I've ran into a weird bug. It won't let me add a new Page, every time i try to do so the InitializeComponent in the constructor gives me an error :

The name 'InitializeComponent' does not exist in the current context

I've added Microsoft.Maui.Controls in the C# code and in the XAML code.

xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

Does anyone know what to do here?

Error message

2 Answers

I got the same error after adding new MAUI page to the project. The solution was to remove the following lines from .csproj file:

<ItemGroup>
  <MauiXaml Remove="Pages\Page1.xaml" />
</ItemGroup>

<ItemGroup>
  <EmbeddedResource Include="Pages\Page1.xaml">
    <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
  </EmbeddedResource>
</ItemGroup>

Then remove InitializeComponent(); statement in Page1.xaml.cs file, rebuild the project and finally add InitializeComponent(); back to Page1.xaml.cs.

The problem is that Maui SourceGen was not able to generate Page1.xaml.sg.cs file for the Page1.xaml.cs file.

Let me know if this solution works for you.

Related