You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

新建.NET MAUI项目编译报错:‘InitializeComponent’在当前上下文中不存在

Fix: ".NET MAUI: 'InitializeComponent' does not exist in the current context" Error

Problem Summary

I was building a .NET MAUI app on Windows 10 Home 20H2 with Visual Studio 2019 v16.11.0 Preview 2.0. I’d followed all official setup guides step-by-step and ran the maui-check CLI tool—every check passed. But when I created a new .NET MAUI project, two stubborn compile errors popped up:

  • The name 'InitializeComponent' does not exist in the current context (in both App.xaml.cs and MainPage.xaml.cs constructors)
  • The name 'CounterLabel' does not exist in the current context (in the button click handler in MainPage.xaml.cs)

Troubleshooting Steps I Tried

I worked through nearly all common fixes suggested for this error:

  • Adding/removing XAML files and restoring them
  • Double-checking namespace consistency (confirmed namespaces in XAML and code-behind matched perfectly):

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(); // Error here
        }
        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(); // Error here
        }
        int count = 0;
        private void OnCounterClicked(object sender, EventArgs e)
        {
            count++;
            CounterLabel.Text = $"Current count: {count}"; // Error here
        }
    }
}

Solution That Worked

The issue boiled down to special characters in my project path. My project was initially stored in a folder like c:\develop\c#\MyMAUIApp. After moving the entire project to a path without special characters (e.g., c:\develop\MyMAUIApp), all compile errors disappeared instantly, and the project built successfully.

Root Cause

It seems the .NET MAUI XAML code generation process can’t handle paths with special characters like # properly. This prevents the auto-generated code for InitializeComponent and control references from being created correctly.

内容的提问来源于stack exchange,提问作者mnemonic

火山引擎 最新活动