我尝试开始使用.Net MAUI,并按照如下步骤设置了我的开发环境:
我还运行了“MAUI”CLI工具,所有内容都已检查完毕,但是当我使用VisualStudio2019 v16.11.0预览2.0 (运行在Windows10Home 20H2上)创建一个新的.NET InitializeComponent应用程序时,我会得到“名称'InitializeComponent‘在当前上下文中不存在”构建错误。它也找不到对表单上任何控件的引用。‘名称'CounterLabel’在当前上下文中不存在‘
我在这篇文章中尝试了几乎所有的名称“InitializeComponent”在当前上下文中不存在,其中包含了一些建议,比如添加和删除文件、进行更改并将它们更改回.基本上,除了往许愿井里扔一分钱外,什么都没有。
我发现一个常见的错误是名称空间不匹配,但下面是我所展示的名称空间是正确的:
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#‘的文件夹中,它就能工作。这显然会导致后台的某些解析失败。
发布于 2022-06-18 08:56:43
同样的错误也出现了,因为我是一个绝对的初学者,并且错过了XAML文件中的error‘>结尾.因此,它的也可能是错误,这导致了这个错误。
https://stackoverflow.com/questions/68046917
复制相似问题