13

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

  1. https://docs.microsoft.com/en-us/dotnet/maui/get-started/first-app?pivots=windows
  2. https://docs.microsoft.com/en-us/windows/apps/project-reunion/set-up-your-development-environment#required-workloads-and-components
  3. https://docs.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.

mnemonic
  • 490
  • 6
  • 17

5 Answers5

21

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.

Geethu Vinod
  • 211
  • 1
  • 4
  • 4
    Also make sure to remove anything listed for 'Custom Tool and Custom Tool Namespace' – ben Jul 05 '21 at 17:51
  • Edit queue is full for this answer, but take in consideration that now it's possible to add a new ContentPage for MAUI within the Add > New Item Menu. Just select under Visual C# Items > .NET MAUI > .NET MAUI ContentPage (XAML). That will also geerate the C# file with template applied. – Juan Ignacio Avendaño Huergo Feb 20 '22 at 15:10
17
  1. Close VS2022
  2. Open VS2022
  3. Open project with menu VS2022(File - Open - Project...)
  • 1
    This turn off/turn on approach did work for me too - but I just closed and opened the solution without closing VS2022. Someone downvoted you, probably because you didn't explain your answer more fully. Personally, I don't know why it worked either. – Rich K Oct 28 '21 at 14:43
  • 1
    Restarting VS22 worked for me. Thanks! Still a problem 4 months later. – michael g Nov 01 '21 at 03:30
  • closing visual studio and taking the project work for me visual studio 2022 17.1 preview. – lava Nov 24 '21 at 12:53
  • Worked for me too closing and opening the entire Visual studio 2022. This should be the accepted answer. – Chandraprakash Jan 16 '22 at 21:53
  • Using VS 2022 Preview 17.1.0 Preview 3.0, and this worked for me too. – Ted Jan 20 '22 at 22:23
2

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.

mnemonic
  • 490
  • 6
  • 17
0

A very simple answer but has happened to me a few times.

Make sure you are using the preview version of VS 2022. It is very easy to accidently open up any other version of VS, which will cause the error to occur.

Knight0fDragon
  • 16,292
  • 2
  • 21
  • 42
  • It happens with VS 2022 preview too. – Shidouuu Jan 29 '22 at 17:00
  • Not sure why you downvoted it since you either didn't open it in preview, or your error is just a different one. Create a new project in MAUI, then open it up in VS 2022 standard, and you will see this error. – Knight0fDragon Jan 29 '22 at 17:04
  • I know I am in VS 2022 preview since it is the only version of VS I have installed... and I can clearly see the PRE icon on my taskbar. – Shidouuu Jan 29 '22 at 17:10
  • 1
    if you only have 2022 preview installed then obviously this answer does not apply to you, so why are you even responding on it and wasting everybodies time. – Knight0fDragon Jan 29 '22 at 17:24
  • Because it's not useful. The only version of VS you can even create a MAUI project in (currently) is VS 2022 preview. – Shidouuu Jan 29 '22 at 17:27
  • Well, if you had more than 1 version of VS installed, you would have understood that the project opens in the release version of VS 2022 by default giving you this error. Try reading answers that actually apply to your situation, you may get better results. – Knight0fDragon Jan 29 '22 at 17:54
-1

I have found a workaround for this problem. I am a newbie to c# and especially to .net Maui therefore, my apologies if I misinterpreted things differently.

First things first The error is about the context that the InitializeComponent() resides in. Context is nothing but the nuget package dependency that the file is looking for.

To change the context

  1. Open the .cs file and at the top of the editor click the navigation bar as shown in the Image
  2. Change the context to Windows specific dependency from the list.

And also the other workaround would be to change the build action of the xaml file to MauiXaml as others mentioned