如果我在Visual Studio 2010 SP1中创建一个新项目,并选择“WPF应用程序”,并尝试构建生成的应用程序,我会得到错误

名称“InitializeComponent”在当前上下文中不存在。

今天早上,当我试图构建当前项目时,我遇到了类似的错误。昨天,我编译和运行它没有问题。

我创建了一个新项目,每当我编译项目时,都会得到错误。我刚刚把项目发给了同事,他刚刚编译完成,没有任何错误。

怎么了?


当前回答

(老问题了,但如果搜索把你带到了这里)

01/09/2022 -在visual studio社区2022经历了问题。

报告的问题是一个未更改的.xaml.cs文件。

今天对我有效的解决方法是:

复制了我一直在做的最后一个xaml文件(和xaml.cs)到一个不在项目树中的文件夹。

在解决方案资源管理器中右键单击文件并选择“删除” (这将删除文件)

重新构建项目(在引用文件的地方会出现错误)

将xaml和xaml.cs文件复制回项目文件夹

右键单击项目,选择“添加/现有项目”,并选择xaml文件。 重新构建项目

(对我来说,它构建和其他xaml文件上的错误已经消失了)

其他回答

我在把沙玛林的溶液转化成毛伊岛。

我需要更改MyPage。xaml文件属性/构建操作

来自“嵌入式资源”(在Xamarin中使用的方式)

到“MauiXaml”(在毛伊岛使用)。

我还必须清除属性/自定义工具。

这有一个非常具体的原因,它在项目设置中。当您试图将WPF控件/窗口添加到. net 2.0类库或项目中时,通常会发生这种情况。出现此错误的原因是项目不知道它正在构建WPF控件或窗口,因此试图将其作为c# 2.0项目构建。

解决方案涉及编辑.csproj文件。右键单击引起问题的项目,选择“卸载项目”。右键单击未加载的项目并选择“Edit .csproj”。将打开.csproj文件,您可以看到XML。看看下面这行:

<Import Project=…..

它在文件的末尾,你仅有的一行大概是

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

这告诉Visual Studio将项目构建为. net 2.0项目。我们要做的是告诉Visual Studio这实际上是一个WPF项目,所以我们必须添加以下一行:

<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />

这一行将告诉Visual Studio将项目构建为WPF项目。现在你的.csproj文件底部应该是这样的:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />

保存.csproj文件,在解决方案资源管理器中右键单击它,并选择“重新加载项目”编译,就是这样,您都完成了!

我遇到过几次这种情况,但总是忘记是什么原因造成的。 当我在文件后面的代码上重命名命名空间时,我遇到了这个问题,但不是在XAML中。

所以检查一下你是否做了同样的事情。

名称空间和类名需要匹配,因为它们都是部分类的一部分

namespace ZZZ
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
         //...
    }
}

<!-- XAML -->
<Window x:Class="ZZZ.MainWindow">

当我试图构建一个针对net48的SDK项目时,这种情况发生在我身上。它与MSBuild.exe一起工作很好,但dotnet构建失败与给定的错误。

对我有用的是

在<PropertyGroup/>节点中添加<UseWPF>true</UseWPF> 删除所有<Page />元素,因为它们现在已被自动包含。

现在msbuild和dotnet都可以构建了。

以下是完整的项目文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{07465991-AC74-4C22-B1DE-7BA67A040631}</ProjectGuid>
    <OutputType>library</OutputType>
    <RootNamespace>MyRootNamespace</RootNamespace>
    <AssemblyName>MyAssemblyName</AssemblyName>
    <FileAlignment>512</FileAlignment>
    <PackagesDirectory>$(UserProfile)\.nuget\packages</PackagesDirectory>
    <ResolveNuGetPackages>true</ResolveNuGetPackages>
    <SkipValidatePackageReferences>true</SkipValidatePackageReferences>
    <TargetFramework>net48</TargetFramework>
    <TargetFrameworkProfile />
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <IsNetCoreProject>false</IsNetCoreProject>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <AutoGenerateBindingRedirect>true</AutoGenerateBindingRedirect>
    <ResolveAssemblyReferencesSilent>true</ResolveAssemblyReferencesSilent>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <UseWPF>true</UseWPF>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>embedded</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>embedded</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml" />
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
</Project>

如果您正在使用Xamarin Forms,并且您移动了一个XAML文件,那么该文件的“构建操作”将被更改。Xamarin Forms要求“构建动作=嵌入式资源”。

在Visual Studio中应用“build action”:

选择XAML文件-> Properties -> Build Action = Embedded Resource