MVC
MVC

Reputation: 669

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

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

Upvotes: 2

Views: 1026

Answers (3)

RedNet
RedNet

Reputation: 52

I had the same thing happen to me, what fixed it was:

  1. Close VS2022
  2. Delete the contents of the OBJ folder
  3. Open VS

Upvotes: 1

Jason Garner
Jason Garner

Reputation: 67

I am posting this solution here because I encountered this issue under very similar circumstances in that I moved my WPF solution from .NET Framework to .NET 7. I also am using VS Community Edition. Version is 2022 17.4.1

In my case I could nuke the obj folder and clean my project, close, and reopen VS and there would be no errors. I could code and intellisense would happily throw me suggestions. However, as soon as I ran my project, the intellisense errors would show up on every single namespace variable assocated with a XAML view.

My XAML pages build actions were set to page and my C# code behind pages build actions were set to C# compiler.

I switched my C# code behind build actions to "Page" and then switched them back to C# Compiler and the issue mysteriously vanished.

I hope this helps someone.

Upvotes: 1

MVC
MVC

Reputation: 669

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.

Upvotes: 5

Related Questions