gpt4 book ai didi

New .Net MAUI App project throws 'The name 'InitializeComponent' does not exist in the current context' build errors(新的.Net Maui应用程序项目引发“”InitializeComponent“”名称在当前上下文中不存在“”生成错误)

转载 作者:bug小助手 更新时间:2023-10-28 13:50:12 27 4
gpt4 key购买 nike



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

我已经尝试开始使用.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


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'

我还运行了‘Maui-Check’CLI工具,一切都已检查完毕,但当我使用Visual Studio2019 v16.11.0预览版2.0(在Windows 10 Home 20H2上运行)创建一个新的.NET Maui应用程序时,我收到了‘The name’InitializeComponent‘is’is‘is not in the Current Context’的构建错误。它也找不到对表单上任何控件的引用。‘名称’CounterLabel‘在当前上下文中不存在’


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.

我在这篇文章中尝试了几乎所有的方法‘InitializeComponent’这个名字在当前的上下文中不存在,它包含了添加和删除文件、进行更改和将它们改回……基本上除了往许愿井里扔一分钱。


I found that a common mistake is a namespace mismatch, but here is what I have showing that the namespaces are correct:

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


App.xaml:

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

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:

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

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.

我创建的项目的路径是c:\Development\c#......一旦我将项目复制到不包含‘c#’的文件夹中,它就可以工作了。这显然会导致后台的某些解析失败。


更多回答

Same problem and same solution > stackoverflow.com/a/68051898/16884226

同样的问题和解决方案>Stackoverflow.com/a/68051898/16884226

优秀答案推荐

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.

我遇到了同样的问题,看起来就像我在VS中创建了一个Content Page一样,它仍然指向Xamarin表单。将名称空间更改为Maui后,我将XAML Page的构建操作(RightClick on XAML Page>>Properties>>BuildAction)更新为MauiXaml,它对我起作用了。




  1. Close VS2022

  2. Open VS2022

  3. Open project with menu VS2022(File - Open - Project...)



What caused it for me was that I had renamed the xaml and xaml.cs file something else but I hadn't updated it in the ContentPage node in the xaml under x:Class

导致我出现这种情况的原因是,我将XAML和xaml.cs文件重命名为其他名称,但没有在x:Class下的XAML的Content Page节点中更新它



The same error popped in for me being an absolute beginner and missing the ending '>' in the XAML file. So, It could also be XAML errors, which leads to this error.

同样的错误突然出现,因为我是一个绝对的初学者,在XAML文件中错过了结尾的‘>’。因此,也可能是XAML错误,从而导致此错误。



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.

我创建的项目的路径是c:\Development\c#......一旦我将项目复制到不包含‘c#’的文件夹中,它就可以工作了。这显然会导致后台的某些解析失败。



I'm using VS 2022 Preview 17.4.0 Preview 1.0, I don't know why but when you create a new Content Page it creates with the wrong namespace. take a look in the C# file and fix the namespace. It worked for me.

我使用的是VS 2022预览版17.4.0预览版1.0,我不知道为什么,但当你创建一个新的内容页面时,它会用错误的命名空间创建。查看C#文件并修复名称空间。这对我很管用。



Make sure you select a .Net MAUI Content Page and not a Content Page

确保您选择的是.Net Maui内容页而不是内容页


Maui ContentPage



Just run visual studio with "run as administrator" and everything is done

只需使用“以管理员身份运行”来运行Visual Studio,即可完成所有操作



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.

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



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.

我找到了解决这个问题的方法。我是一个C#新手,尤其是.net毛伊岛,因此,如果我误解了不同的东西,我道歉。


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.

首先,错误与InitializeComponent()所在的上下文有关。上下文只是文件正在查找的Nuget包依赖项。


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

另外,另一种解决方法是将XAML文件的构建操作更改为MauiXaml,正如前面提到的



I am using preview version of 17.5 VS2022, I add to do below changes to run the app after adding new page.

我使用的是预览版17.5 VS2022,我添加做下面的更改添加新页面后运行应用程序.


*.Xmal Build Action -> BundleResource

*.Xmal构建操作->BundleResource


*.Xmal.cs Build Action -> Compile

*.XMal.cs生成操作->编译


My VS Version


New MAUI XAML Page


Xmal Build -> BundleResource


Xmal.cs Build -> Compile



For me the problem was that I've probably accidentally pressed "Exclude From Project" on MauiProgram.cs file. Hence the error.
Don't know how it could've happened. Probably clicked on the file when was deleting files that lies beside. I've spend some hours looking for the reason for this error.
It was my first ever VS Code, C#/.NET MAUI project.

对我来说,问题是我可能不小心按下了MauiProgram.cs文件上的“从项目中排除”。因此出现了错误。不知道怎么会发生这种事。可能在删除旁边的文件时点击了该文件。我花了几个小时寻找这个错误的原因。这是我的第一个VS代码,C#/.NET Maui项目。


Just have been following the tutorial. Made all the input myself, never copypasted. And somewhere on the Step 4 it stopped working. So I've thought that maybe I made a typo somewhere.

只是一直在跟着教程走。所有的输入都是我自己做的,从来没有抄袭过。在第四步的某个地方,它停止了工作。所以我想也许我在什么地方打错了字。


Downloaded the final code from the tutorial and started comparing files. After sometime, I've noticed the difference in .csproj file. Bingo! The MainPage files supposed to be deleted, not just excluded from project. And MauiProgram.cs was excluded by mistake. At first I didn't even know how this lines got in the file. Than I've learned about "Exclude From Project". After including MauiProgram.cs back in the project it started working as expected.

已下载教程中的最终代码并开始比较文件。过了一段时间,我注意到.csproj文件中的不同之处。对啰!主页面文件应该被删除,而不仅仅是从项目中排除。而MauiProgram.cs被错误地排除在外。起初,我甚至不知道这些行是如何出现在文件中的。比我所了解的“排除在项目之外”还多。在将MauiProgram.cs重新纳入项目后,它开始像预期的那样工作。


    <ItemGroup>
<Compile Remove="MainPage.xaml.cs" />
<Compile Remove="MauiProgram.cs" />
</ItemGroup>

<ItemGroup>
<MauiXaml Remove="MainPage.xaml" />
</ItemGroup>

Tutorial

补习



Check if there are any errors in the xaml markup or any missing namespace reference in it. For me, I was migrating from Xamarin Forms to MAUI, some of the xml namespaces I was using were from Xamarin Forms such as extended properties from Xamarin Forms infinite scrolling Xamarin.Forms.Extended.InfiniteScrolling.

检查XAML标记中是否有任何错误或其中是否有任何缺少的命名空间引用。对我来说,我是从Xamarin Forms迁移到Maui的,我使用的一些XML名称空间来自Xamarin Forms,例如来自Xamarin Forms无限滚动Xamarin.Forms.Extende.InfiniteScrolling的扩展属性。


So, it was being used like this in a listview:

因此,它在列表视图中是这样使用的:


<ListView.Behaviors>
<extended:InfiniteScrollBehavior IsLoadingMore="{Binding IsBusy}" x:Name="IsLoadingMore" />
</ListView.Behaviors>

Removing the reference and this line fixed the issue. Well, of-course I need to re-write this functionality.

删除引用,此行已修复该问题。当然,我需要重写这个功能。



And another rather rare case, i had

还有一个相当罕见的病例,我有


xmlns:draw="http://schemas.appomobi.com/drawnUi/2023/draw"

Xmlns:draw=“http://schemas.appomobi.com/drawnUi/2023/draw”


and that xmlns was defined in the same assembly that was failing to compile, with the error in question. So needed to change to

并且该xmlns是在编译失败的同一个程序集中定义的,但出现了相关的错误。因此需要更改为


xmlns:draw="using:AppoMobi.Maui.DrawnUi.Draw"

xmlns:draw=“using:AppoMobi.Maui.DrawnUi.Draw”


and it went working.

而且它起作用了。


更多回答

Also make sure to remove anything listed for 'Custom Tool and Custom Tool Namespace'

另外,请确保删除任何列出的“自定义工具和自定义工具空间”

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.

对于这个答案,编辑队列已满,但考虑到现在可以在Add>New Item菜单中为Maui添加一个新的Content Page。只需在Visual C#Items>.NET Maui>.NET Maui Content Page(XAML)下选择。这也将生成应用了模板的C#文件。

Here I am 2 years+ later and I still have this problem. This answer worked like a charm.

两年多后的今天,我仍然有这个问题。这个回答真是妙不可言。

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.

这种关闭/打开方法对我也有效-但我只是关闭并打开了解决方案,而没有关闭VS2022。有人否决了你,可能是因为你没有更充分地解释你的答案。就个人而言,我也不知道它为什么会奏效。

Restarting VS22 worked for me. Thanks! Still a problem 4 months later.

重新启动VS22对我来说很有效。谢谢!4个月后,这仍然是个问题。

closing visual studio and taking the project work for me visual studio 2022 17.1 preview.

关闭VisualStudio2022 17.1预览版并接受项目工作。

Worked for me too closing and opening the entire Visual studio 2022. This should be the accepted answer.

对我来说,关闭和打开整个Visual Studio 2022也很有效。这应该是公认的答案。

Using VS 2022 Preview 17.1.0 Preview 3.0, and this worked for me too.

使用VS 2022预览版17.1.0预览版3.0,这对我来说也是有效的。

If you move your page from "/" into a "/View" folder, this is the answer you need.

如果您将页面从“/”移到“/View”文件夹,这就是您需要的答案。

LOL. This one hurts -- but that was my problem.

LOL。这个很疼--但那是我的问题。

I had the same thing happen. I think mine was an issue with the xaml "ContentPage" values.

我也经历过同样的事情。我认为我的问题是XAML“Content Page”值的问题。

Thanks! I'm using the preview also. When I added a new .NET MAUI ContentView (XAML) in the Views folder and it added the ".Views" to the end of the namespace, even tho all the other files in the Views folder don't have ".Views". I removed it and it works now!

谢谢!我也在用预览版。当我在Views文件夹中添加一个新的.NET Maui内容视图(XAML)时,它在名称空间的末尾添加了“.Views”,尽管Views文件夹中的所有其他文件都没有“.Views”。我把它取下来了,现在它起作用了!

It happens with VS 2022 preview too.

VS 2022预览版也会发生这种情况。

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.

不知道为什么你否决了它,因为你要么没有在预览中打开它,要么你的错误只是另一个错误。在毛伊岛创建一个新项目,然后在VS 2022标准中打开它,您将看到这个错误。

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.

我知道我在VS 2022预览版,因为这是我安装的VS的唯一版本…我可以清楚地看到任务栏上的PRE图标。

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.

如果你只安装了2022预览版,那么这个答案显然不适用于你,那么你为什么还要回应它,浪费大家的时间呢?

Because it's not useful. The only version of VS you can even create a MAUI project in (currently) is VS 2022 preview.

因为它没有用。您甚至可以在VS中创建毛伊岛项目的唯一版本(目前)是VS 2022预览版。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com