首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >新的.Net MAUI项目抛出“当前上下文中不存在的名称'InitializeComponent‘”构建错误

新的.Net MAUI项目抛出“当前上下文中不存在的名称'InitializeComponent‘”构建错误
EN

Stack Overflow用户
提问于 2021-06-19 12:48:04
回答 9查看 9.3K关注 0票数 19

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

  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

我还运行了“MAUI”CLI工具,所有内容都已检查完毕,但是当我使用VisualStudio2019 v16.11.0预览2.0 (运行在Windows10Home 20H2上)创建一个新的.NET InitializeComponent应用程序时,我会得到“名称'InitializeComponent‘在当前上下文中不存在”构建错误。它也找不到对表单上任何控件的引用。‘名称'CounterLabel’在当前上下文中不存在‘

我在这篇文章中尝试了几乎所有的名称“InitializeComponent”在当前上下文中不存在,其中包含了一些建议,比如添加和删除文件、进行更改并将它们更改回.基本上,除了往许愿井里扔一分钱外,什么都没有。

我发现一个常见的错误是名称空间不匹配,但下面是我所展示的名称空间是正确的:

App.xaml:

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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#‘的文件夹中,它就能工作。这显然会导致后台的某些解析失败。

EN

Stack Overflow用户

发布于 2022-06-18 08:56:43

同样的错误也出现了,因为我是一个绝对的初学者,并且错过了XAML文件中的error‘>结尾.因此,它的也可能是错误,这导致了这个错误。

票数 3
EN
查看全部 9 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68046917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档