Search code examples
c#xamlmaui.net-7.0

.NET MAUI: The name 'InitializeComponent' does not exist in the current context


I have created a ResourceDictionary file that comes with a .cs file. So after creating the ResourceDictionary file I'm getting an error:

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

Error Screenshot

Below is the XAML file associated with the above file.

enter image description here

I'm using VS2022 17.5.0 Preview 1.0

  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

Solution

  • When I do Project / rt-click / Add / NewItem / .Net Maui ResourceDictionary (XAML), this is what gets added to .csproj:

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

    Note that the Compile is nested within the MauiXaml element; it is not a separate Item.

    See if your .csproj has TWO separate Items, one for Dictionary1.xaml, another for Dictionary1.xaml.cs.

    If so, replace those two items, with a combined item as shown above.

    (For me, this builds without problem.)


    In your case, the .csproj doesn't show MauiXaml BuildAction?

    Replace:

    <ItemGroup>
      <None Include="Themes\Light.xaml" />
    </ItemGroup>
    

    with:

    <ItemGroup>
        <MauiXaml Update="Themes\Light.xaml">
            <Generator>MSBuild:Compile</Generator>
        </MauiXaml>
    </ItemGroup>
    

    I don't know whether this should be kept or removed:

    <ItemGroup>
      <Folder Include="Themes\" />
    </ItemGroup>
    

    Leave it in for now.