Search code examples
c#xamlmaui

New .Net MAUI App project throws 'The name 'InitializeComponent' does not exist in the current context' build errors


I've attempted to play with .Net MAUI and I've setup my development environment following the steps as described in:

  1. https://learn.microsoft.com/en-us/dotnet/maui/get-started/first-app?pivots=windows
  2. https://learn.microsoft.com/en-us/windows/apps/project-reunion/set-up-your-development-environment#required-workloads-and-components
  3. https://learn.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/device-manager?tabs=windows&pivots=windows

I've also run the 'maui-check' CLI tool and everything checks out, but when I create a new .NET MAUI App with Visual Studio 2019 v16.11.0 Preview 2.0 (running on Windows 10 Home 20H2), I get the 'The name 'InitializeComponent' does not exist in the current context' build errors. It also doesn't find the references to any controls on the form e.g. 'The name 'CounterLabel' does not exist in the current context'

I've tried almost everything in this post The name 'InitializeComponent' does not exist in the current context which contains suggestions like adding and removing files, making changes and changing them back... basically everything except throwing a penny in a wishing well.

I found that a common mistake is a namespace mismatch, but here is what I have showing that the namespaces are correct:

App.xaml:

<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiApp1"
             x:Class="MauiApp1.App">
    ...
</Application>

App.xaml.cs

using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using System;
using Application = Microsoft.Maui.Controls.Application;

namespace MauiApp1
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent(); <-- This is throwing the build error...
        }

        protected override IWindow CreateWindow(IActivationState activationState)
        {
            this.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>()
                .SetImageDirectory("Assets");

            return new Microsoft.Maui.Controls.Window(new MainPage());
        }
    }
}

MainPage.xaml:

ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}">

            ...
</ContentPage>

MainPage.xaml.cs

using System;
using Microsoft.Maui.Controls;

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent(); <-- This is throwing the build error...
        }

        int count = 0;
        private void OnCounterClicked(object sender, EventArgs e)
        {
            count++;
            CounterLabel.Text = $"Current count: {count}"; <-- This is throwing the build error...
        }
    }
}

Any help will be greatly appreciated!

---=== UPDATE ===---

The path to the project I created is c:\develop\c#...... as soon as I copy the project to a folder that doesn't contain 'c#' it works. This clearly causes some parsing in the background to fail.


Solution

  • I faced the same issue and looks like when I created a ContentPage in VS its still pointing to Xamarin Forms. After changing namespace to MAUI, I updated the Build Action(RightClick on Xaml Page>>Properties>>BuildAction) of XAML Page to MauiXaml and it worked for me.