首页 > 解决方案 > 我的 WPF 应用程序的当前上下文中不存在名称“InitializeComponent”

问题描述

我最近将我的 .NET Framework 4.8 WPF 应用程序转换为 .NET5。当我编译应用程序时,出现以下错误

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

我关注了这个链接,但对我没有多大帮助。

我检查了两者*.xaml*.cs文件命名空间。

<Window x:Class="Cl.Wpf.MessageWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent = "WidthAndHeight"
        Title="MessageWindow" >

还检查了.cs文件

namespace Cl.Wpf
{
    public partial class MessageWindow : Window
    {                     

        public MessageWindow()
        {
            InitializeComponent();
             .....
        }

我的*.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <OutputType>Library</OutputType>
    <RootNamespace>Cl.Wpf</RootNamespace>
    <Deterministic>false</Deterministic>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWPF>true</UseWPF>
    <ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.2.241603">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <ItemGroup>
    <Compile Update="Properties\Settings.Designer.cs">
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <None Update="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>
</Project>

我还尝试删除obj文件夹并重新启动 Visual Studio 2019,但也没有帮助我。

另外,尝试在XAML文件中添加一些空间,但这也无济于事。

xml

构建动作MessageWindow.xml

MessageWindow.xml 的构建操作

标签: c#wpfvisual-studio-2019

解决方案


似乎问题出在Microsoft Visual Studio Community 2019 Version 16.11.2.

我重新安装了社区版本,但没有再次工作。

所以最后,我卸载了社区版本并重新安装Microsoft Visual Studio Professional 2019 Version 16.11.2,幸运的是它对我有用。

我不确定这对其他用户有多大用处,但这对我有用。


推荐阅读