2

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

MVC
  • 649
  • 7
  • 23
  • Could be you need to edit the project file and add `true`. Also `net5.0-windows` as TF. For details see: https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/sdk-and-target-framework-change – lidqy Aug 22 '21 at 10:04
  • Hi @lidqy, `true` is already there in the project file. – MVC Aug 22 '21 at 10:10
  • Hhm. I don't know. Project file looks okay to me. Do the file names match: `MessageWindow.xaml.` and `MessageWindow.xaml.cs` Is the CSharp-file displayed as a "child" of the XAML file in the Solution explorer? – lidqy Aug 22 '21 at 11:25
  • Also : do all dependencies resolve? Do you have yellow icons over one of the entries? – lidqy Aug 22 '21 at 11:32
  • And one more : What's your VS 2019 version? It seems there were issues in older releases that caused that bug with InitializeComponent ... https://developercommunity.visualstudio.com/t/wpf-project-on-net-core-3-the-name-initializecompo/745596 – lidqy Aug 22 '21 at 11:36
  • Hi @lidqy, I have attached screenshot in the question. yes there is no error in the dependencies section. My visual studio version is `Microsoft Visual Studio Community 2019 Version 16.11.0`. – MVC Aug 22 '21 at 11:42
  • OK, last one: What's the "Build Action" for "MessageWindows.xaml" in the property tab for that file? Should be "Page" – lidqy Aug 22 '21 at 12:14
  • Ensure that the Build Ation of the MainWindow.xaml file is set to Page. – BionicCode Aug 22 '21 at 12:38
  • Add a new window to the project. Move/copy the contents over from the existing file and then delete the existing file. – mm8 Aug 23 '21 at 14:54
  • Hi @lidqy, The `Build Action` for `MessageWindows.xml` is `Page`. I tried creating new window to the same project but nothing helped. – MVC Aug 26 '21 at 11:41
  • @BionicCode, `Build Action` for `MessageWindows.xml` is Page only. I also have attached screenshot. – MVC Aug 26 '21 at 11:42
  • @mm8 I tried creating new window to the same project but nothing helped. I also even tried creating a new wpf solution but still I am getting the same issue. – MVC Aug 26 '21 at 11:43

2 Answers2

5

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.

MVC
  • 649
  • 7
  • 23
0

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.