The name 'InitializeComponent' does not exist in the current context in my WPF application

Viewed 218

I have recently converted my .NET Framework 4.8 WPF application to .NET5. When I compile the application, I am get below error

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

I followed this link but couldn't helped me much.

I checked both *.xaml and *.cs file namespace.

<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" >

Also checked .cs file

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

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

My *.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>

I also tried deleting obj folder and restarted visual studio 2019 but also did not help me out.

Also, tried adding some space in the XAML file but that didn't help as well.

xaml

Build Action MessageWindow.xml

Build Action for MessageWindow.xml

1 Answers

It seems that the issue is with Microsoft Visual Studio Community 2019 Version 16.11.2.

I reinstalled the community version but did not work again.

So finally, I uninstalled community version and reinstalled Microsoft Visual Studio Professional 2019 Version 16.11.2 and fortunately it worked for me.

I am not sure how useful this would be for other users but this worked for me.

Related