【问题标题】:New .Net MAUI App project throws 'The name 'InitializeComponent' does not exist in the current context' build errors新的 .Net MAUI App 项目抛出“当前上下文中不存在名称‘InitializeComponent’”构建错误
【发布时间】:2021-09-03 21:14:45
【问题描述】:

我已尝试开始使用 .Net MAUI,并按照以下步骤设置了我的开发环境:

  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

我还运行了“maui-check”CLI 工具并检查了所有内容,但是当我使用 Visual Studio 2019 v16.11.0 Preview 2.0(在 Windows 10 Home 20H2 上运行)创建新的 .NET MAUI 应用程序时,我获取“当前上下文中不存在名称‘InitializeComponent’”构建错误。它也没有找到对表单上任何控件的引用,例如'当前上下文中不存在名称'CounterLabel''

我已经尝试了这篇 The name 'InitializeComponent' does not exist in the current context 帖子中的几乎所有内容,其中包含添加和删除文件、进行更改和更改回文件等建议...基本上所有内容,除了向许愿井扔一分钱。

我发现一个常见的错误是命名空间不匹配,但这里显示的是命名空间是正确的:

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

任何帮助将不胜感激!

---=== 更新 ===---

我创建的项目的路径是 c:\develop\c#...... 只要我将项目复制到不包含“c#”的文件夹,它就可以工作。这显然会导致某些后台解析失败。

【问题讨论】:

标签: c# xaml maui


【解决方案1】:

我创建的项目的路径是 c:\develop\c#...... 只要我将项目复制到不包含“c#”的文件夹,它就可以工作。这显然会导致某些后台解析失败。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,看起来当我在 VS 中创建 ContentPage 时它仍然指向 Xamarin Forms。将命名空间更改为 MAUI 后,我将 XAML 页面的 Build Action(RightClick on Xaml Page>>Properties>>BuildAction) 更新为 MauiXaml,它对我有用。

    【讨论】:

    • 还要确保删除为“自定义工具和自定义工具命名空间”列出的任何内容
    • 此答案的编辑队列已满,但请注意,现在可以在“添加”>“新项目”菜单中为 MAUI 添加新的 ContentPage。只需在 Visual C# Items > .NET MAUI > .NET MAUI ContentPage (XAML) 下选择。这也将生成应用模板的 C# 文件。
    【解决方案3】:
    1. 关闭VS2022
    2. 打开VS2022
    3. 使用菜单 VS2022(文件 - 打开 - 项目...)打开项目

    【讨论】:

    • 这种关闭/打开方法也对我有用 - 但我只是关闭并打开了解决方案而没有关闭 VS2022。有人对你投了反对票,可能是因为你没有更充分地解释你的答案。就个人而言,我也不知道它为什么会起作用。
    • 重启 VS22 对我有用。谢谢! 4 个月后仍然存在问题。
    • 关闭 Visual Studio 并为我工作 Visual Studio 2022 17.1 预览版。
    • 也为我工作了关闭和打开整个 Visual Studio 2022。这应该是公认的答案。
    • 使用 VS 2022 Preview 17.1.0 Preview 3.0,这对我也有用。
    【解决方案4】:

    一个非常简单的答案,但发生在我身上几次。

    请确保您使用的是VS 2022的预览版。很容易意外打开任何其他版本的VS,这将导致错误发生。

    【讨论】:

    • VS 2022 预览版也会发生这种情况。
    • 不知道为什么你对它投了反对票,因为你要么没有在预览中打开它,要么你的错误只是一个不同的错误。在MAUI中新建一个项目,然后用VS 2022标准打开,就会看到这个错误。
    • 我知道我在 VS 2022 预览版中,因为它是我安装的唯一 VS 版本...而且我可以清楚地看到我的任务栏上的 PRE 图标。
    • 如果您只安装了 2022 预览版,那么显然这个答案不适用于您,那么您为什么还要回复它并浪费大家的时间。
    • 因为没用。 (目前)您甚至可以在其中创建 MAUI 项目的唯一 VS 版本是 VS 2022 预览版。
    猜你喜欢
    • 1970-01-01
    • 2012-08-03
    • 2011-10-19
    • 2016-06-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多