简体   繁体   English

当前上下文中不存在名称“InitializeComponent”(c#,xamarin)

[英]The name 'InitializeComponent' does not exist in the current context(c#,xamarin)

I just downloaded Visual Studio and created a new Xamarin.Forms project.我刚刚下载了 Visual Studio 并创建了一个新的 Xamarin.Forms 项目。 However, when building the app with the auto-generated code I got stuck on the following error in the code-behind of MainPage.xaml:但是,在使用自动生成的代码构建应用程序时,我在 MainPage.xaml 的代码隐藏中遇到了以下错误:

The name 'InitializeComponent' does not exist in the current context

I looked up solutions on the inte.net but did not find one that works for me.我在 inte.net 上查找了解决方案,但没有找到适合我的解决方案。 What I tried:我尝试了什么:

  • Changing the custom tool in the properties of MainPage.xaml (which was empty beforehand)在 MainPage.xaml 的属性中更改自定义工具(之前为空)

  • I checked if the namespaces match (they do)我检查了命名空间是否匹配(它们匹配)

  • I tried saving all the files我尝试保存所有文件

Here is an image of the auto-generated code, which I didn't edit:这是自动生成的代码的图像,我没有编辑它: MainPage.xaml 和 MainPage.xaml.cs

I've had this issue crop up here and there where InitializeComponent and a number of other classes and methods "does not exist in the current context".我在这里和那里都出现了这个问题,其中InitializeComponent和许多其他类和方法“在当前上下文中不存在”。 Not sure of the underlying problem, but if it is the same as I've had, I unload and reload all projects.不确定潜在的问题,但如果它和我遇到的一样,我卸载并重新加载所有项目。 This can be accomplished by right clicking on the project in the solution explorer, clicking unload, right clicking again, and hitting reload.这可以通过右键单击解决方案资源管理器中的项目,单击卸载,再次右键单击,然后单击重新加载来完成。

I had also this problem sometimes when creating a new page, make sure that the BuildAction of the page is set to "Embedded resource" as other normal pages我有时在创建新页面时也遇到这个问题,请确保将页面的 BuildAction 设置为“嵌入资源”作为其他普通页面在此处输入图片说明

If you are having successful builds, but a nagging initializecomponent does not exist error,如果您的构建成功,但出现了 initializecomponent does not exist 错误,

CLOSE VS, DELETE the hidden .vs folder in your project (this clears intellisense).关闭 VS,删除项目中隐藏的 .vs 文件夹(这会清除智能感知)。 OPEN VS - the error is gone. OPEN VS - 错误消失了。

This is what I did to remove the error on my Xamarin project.这就是我为消除 Xamarin 项目中的错误所做的工作。 I closed the project and removed obj folders.我关闭了项目并删除了 obj 文件夹。 Then reloaded the Project again and the error disappeared.然后再次重新加载项目,错误消失了。

Check the ContentPage property of your XAML file.检查 XAML 文件的 ContentPage 属性。 Make sure that the x:Class value, is correct, example: x:Class="YourProjectName.Views.ContentPageName">确保 x:Class 值正确,例如:x:Class="YourProjectName.Views.ContentPageName">

I created a ContentPage and then renamed it, but the x:Class wasn't updated, so I had to do it manually.我创建了一个 ContentPage 然后重命名了它,但是 x:Class 没有更新,所以我不得不手动完成。 Tried first unload/reload but didn't work in my case.首先尝试卸载/重新加载,但在我的情况下不起作用。

In one case also, I created a content page with the name 'Buttons' and had the same issue.在一种情况下,我创建了一个名为“Buttons”的内容页面,但遇到了同样的问题。 I deleted it and created another one with the name 'UsingButtons' and was ok.我删除了它并创建了另一个名为“UsingButtons”的按钮,一切正常。 Maybe there are reserved words for file names.也许有文件名的保留字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# 不包含“InitializeComponent”的定义,名称“controlName”在当前上下文中不存在 - c# does not contain a definition for "InitializeComponent" and name "controlName" does not exist in current context InitializeComponent 在当前上下文中不存在,使用 Mono C# 编译器 - InitializeComponent does not exist in the current context, using Mono C# compiler 名称InitializeComponent在当前上下文中不存在 - The name InitializeComponent does not exist in the current context “当前上下文中不存在名称‘DisplayAlert’。” Xamarin C# - “The name ”DisplayAlert“ does not exist in current context.” Xamarin C# 出现错误:“名称 InitializeComponent 在当前上下文中不存在”,在每个 xaml.cs class(Xamarin Forms)中 - Getting error: "The name InitializeComponent does not exist in the current context", in every xaml.cs class (Xamarin Forms) 当前上下文中不存在“ InitializeComponent” - 'InitializeComponent' does not exist in the current context 我的 WPF 应用程序的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in my WPF application WPF应用程序中的当前上下文中不存在名称“InitializeComponent” - The name 'InitializeComponent' does not exist in the current context in WPF application .NET Maui 当前上下文中不存在名称“InitializeComponent” - .NET Maui The name 'InitializeComponent' does not exist in the current context C# - 当前上下文中不存在该名称 - C# - The name does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM