获取错误:“名称InitializeComponent不存在于目前的内容中”,在每个xaml.cs类别中(Xamarin表单)

rkttyhzu  于 2022-12-07  发布在  其他
关注(0)|答案(6)|浏览(164)

So suddenly the common error "The name InitializeComponent does not exist in the current context" appeared in every single xaml.cs class in all my solution, I have tried multiple common solutions to this issue but none of them seems to work.
A xaml.cs inheriting a Content view:

using System;
using System.Collections.Generic;
using Xamarin.Forms;

namespace FirstApp.Renderers
{
    public partial class CustomExpansibleMenu : ContentView
    {
        public CustomExpansibleMenu()
        {
//Error here
            InitializeComponent();
        }
    }
}

A xaml.cs inheriting from a Content Page:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FirstApp.Services;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace FirstApp.Pages
{
    public partial class ChatMenuPage : ContentPage
    {
        private NavigationService navigation;
        public static double numPixelsW;
        public static double numPixelsH;

        public ChatMenuPage()
        {
            numPixelsH = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;
            numPixelsW = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;

            navigation = new NavigationService();

//Error here 
            InitializeComponent();

//Another error here, searchBox is the name of a frame in my xaml page, but //I cant access to it because of the previous error
            searchBox.BorderWith = 3;
        }
    }
}

These are the common solutions that I have tried:

  1. Verify that the xaml´s buildActions are set to EmbeddedResource 2)Verify that the namespace in xaml and xaml.cs are the same.
  2. In the xaml verify that Custom Tool: MSBuild:UpdateDesignTimeXaml
6jjcrrmo

6jjcrrmo1#

1年零4个月后,我发现这是一个幽灵错误,也就是说,问题是其他东西,最终显示给我们的一个在这篇文章中讨论。那么,什么和在哪里是实际的错误,我们如何解决它?嗯...坏消息,它可以造成许多,许多错误。这些是我所面临的,虽然还有更多(我不会包括他们,因为我没有经历过)。
1.**错误:**首先验证这不是一个错误,要做到这一点,在XAML文件中做一个小的更改,并保存它(一个空间+保存就足够了),如果问题得到解决,恭喜你,如果没有继续阅读,祝你好运...
1.XAML.cs文件的命名空间与XAML文件的x:class不匹配:这是迄今为止最常见的错误,也是我见过的唯一一个错误没有显示为ghost错误的情况。检查所有页面,如果不匹配,请更改名称空间或x:class。
1.**页的生成操作或自定义工具设置不正确:**在这种情况下,存在另一个容易识别的问题。若要检查这两个属性是否设置正确,请右键单击该页,转到“属性,”并验证“生成操作”是否设置为EmbeddedResource,"“自定义工具”是否设置为MSBuild:UpdateDesignTimeXaml。
1.**解决方法:**如果前面的解决方案不适合您,事情可能会变得复杂,这就是为什么在继续使用下面的解决方案之前,请确保执行以下操作:1)删除仓库bin & obj文件夹中的所有文件,2)清理项目+重建,3)重新启动计算机。有时候这样可以解决问题(我不知道为什么),不用担心,这些文件并不重要。
1.**Common .csproj正在隐藏或删除XAML或XAML.cs文件:**天知道为什么有时会在您的Common .csproj文件中添加“〈Folder delete”(它可能与删除类似,例如隐藏或删除),从而从解决方案中删除一些XAML和XAML.cs文件(不用担心,这些文件很好)。要解决此问题,只需删除以下行。
1.**发现同一依赖程序集的不同版本之间存在冲突:**在这种情况下,您可能会将此作为警告或错误。为了解决此问题,我在我的.“csproj”文件中添加了一些行。它将使您重新加载项目,一旦发生这种情况,您可以将其删除(甚至可能被迫这样做)。除非您100%确定这种情况确实发生在您身上,否则我不建议您这样做,原因是我不知道这有什么作用,几个月前我在一个博客上发现了它:

<Target Name="GetDependencyTargetPaths" AfterTargets="ResolvePackageDependenciesForBuild">
    <ItemGroup>
      <TargetPathWithTargetPlatformMoniker Include="@(ResolvedCompileFileDefinitions)" IncludeRuntimeDependency="false" />
    </ItemGroup>
  </Target>

<PackageReference Include="System.Reflection.Emit">
      <Version>4.3.0</Version>
      <ExcludeAssets>All</ExcludeAssets>
      <IncludeAssets>none</IncludeAssets>
</PackageReference>

正如我所说的,这是一个幽灵错误,它可能发生在库之间的几个冲突,由Visual Studio随机更改您的文件属性,等等,因此,这些解决方案可能没有为您工作.如果您用任何其他方法解决了它,请添加到这篇文章,它将是非常有帮助的每个人.
如果你仍然有这个问题,你可以在其他帖子上找到更多的信息,所以继续寻找他们,你最终会找到解决方案,祝你好运。(:

q8l4jmvw

q8l4jmvw2#

我有这个问题,这是因为安装了一个错误的包,删除后,问题解决了。

k2fxgqgv

k2fxgqgv3#

我在我的WPF项目中经常看到这种情况。IntelliSense显示“error”“The name”InitializeComponent“does not exist in the current context”(名称“InitializeComponent”在当前上下文中不存在)”,并显示相同的错误(CS 0103)。但是项目的建置和执行没有问题。如果我将游标停留在InitializeComponent上(),选择“显示潜在的修复程序”,并接受建议“生成方法”MainWindow. InitializeComponent '“”,然后将IntelliSense“错误”(红色波浪线)消失了。但是当我试图构建我的项目时,我得到了构建错误“CS 0111类型'MainWindow'已经用相同的参数类型定义了一个名为'InitializeComponent'的成员”。@Jason的建议有时有效,但不总是有效。由于错误不是'真实的',并且不影响构建,我还没有深入调查。如果这个bug能得到修复就好了。

ruoxqz4g

ruoxqz4g4#

在xaml中没有指向代码隐藏的正确xaml.cs类,例如:
MyView.xaml必须指向内容页上的MyView.xaml.cs,如

ecfsfe2w

ecfsfe2w5#

在我的例子中,我将构建操作更改为MauiXaml,现在可以工作了。

yizd12fk

yizd12fk6#

删除obj文件夹并重建-对我来说已经成功了...

相关问题