Home > Enterprise >  .NET Maui The name 'InitializeComponent' does not exist in the current context
.NET Maui The name 'InitializeComponent' does not exist in the current context

Time:11-29

I have created a ResourceDictionary file that comes with a .cs file. So afer 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 nothing seems to work

CodePudding user response:

This sounds like a similar issue that often plagues Xamarin solutions. I think that there's an underlying bug, or delay in interpreting the files when displayed within Visual Studio.

Several options:

  • Make a small change to the file in question - even adding or deleting a space will do, save it and rebuild solution.
  • Clean the solution and rebuild.
  • Unload and reload project to which the files belong. (Right click on the project in Solution explorer window, select unload, then select load.
  • Xaml files may need to be set as embedded resource as their Build Action under their properties (again in Solution explorer).

CodePudding user response:

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.)

  • Related