Skip to content

[WPF] VSCode shows error for WPF project: "The name 'InitializeComponent' does not exist in the current context (Roslyn CS0103) #5958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
HyVong007 opened this issue Jul 26, 2023 · 41 comments

Comments

@HyVong007
Copy link

1/ Install .NET Core 7.0
2/ Create WPF project with "dotnet new wpf"
3/ VSCode > Open Folder.

After awhile, VSCode shows many errors:

"The name 'InitializeComponent' does not exist in the current context"
Moreover, any variables declared in .XAML file will cause error if these variables are used in .CS file
Hope VSCode Dev Team will fix bugs soon, thank you very much !

@heartacker
Copy link
Contributor

same there

@JoeRobich
Copy link
Member

@HyVong007 @heartacker A couple questions to help narrow down which component might be responsible. Are you using the prerelease C# extension (2.x)? Do you also have the C# DevKit extension installed?

@HyVong007
Copy link
Author

@HyVong007 @heartacker A couple questions to help narrow down which component might be responsible. Are you using the prerelease C# extension (2.x)? Do you also have the C# DevKit extension installed?

The first time i use C# dev kit (release) and C# (release) extensions => everything works fine !

Then C# Dev Kit shows me a notification that i must upgrade C# extension to pre-release version. After that, WPF has error "InitializeComponent does not exist......"

The notification is here:
image

@fbiwarning
Copy link

I have the same errror after upgrade the C# extension to pre-release version. After I switch the c# extension to the release version the error disappeared.

@nesteg
Copy link

nesteg commented Sep 9, 2023

I have the same errror with C# Dev Kit v0.4.10.

@marcin-przywoski
Copy link

I have the exact same issue as the person above on the same version of the Dev Kit. I had the same error on the previous versions as well though
image

@Cryptome2
Copy link

same issue. Any solution?

@sauntheninja2
Copy link

Seeing the same error. Is there any workaround to fix this issue? Tried searching over the internet but all the answers there were related to this error occurring in Visual Studio and not Visual Studio Code.

@engrusamajaved
Copy link

Same issue,
Is there any way to stop these suggestions in VS code, I'm get rid of these error lines.

@awa5114
Copy link

awa5114 commented Oct 18, 2023

Same issue here. It doesn't happen in Visual Studio and compiles fine but somehow VS code throws the CS0103 error for a fresh wpf app created using dotnet new wpf

image

@Kokujou
Copy link

Kokujou commented Oct 19, 2023

same for me. i already tried using omnisharp and preview versions and the dev toolkit, it's all the same. also no autocomplete for XAML but this might be another typic/plugin

@EthanXie0612
Copy link

I have the same issue, the wpf project can run normally, but vscode seems to be unable to associate xaml with xaml.cs properly, which is not an issue in Visual Studio

@bubuwang007
Copy link

Has anyone solved this problem? My OCD is acting up.

@vwnd
Copy link

vwnd commented Nov 23, 2023

I have the same.

@mart1058
Copy link

Still seeing this issue with C# version v2.14.8 and C# Dev Kit v1.1.16. Everything builds and runs OK but the error is still there. Any guidance at this point?

@MelDommer
Copy link

I am seeing this same thing. Project builds in Visual Studio but in vscode it says InitializeCompnent() doesn't exist. (I should add this is a net472 sdk project) The C# dev kit release and pre-release show this for me.

@Matt-Youlend
Copy link

Matt-Youlend commented Feb 23, 2024

Still broken, it has been months.

@Brentec-ca
Copy link

I am also seeing the same symptoms. It affects the hot reload functionality since an error is detected. You are then forced to stop the debugger and rebuild your project. Note that these errors are not preventing the building or running of the solution from scratch.
I'm experiencing this in Visual Studio 2022 with a .NET 6 WPF solution consisting of 5 projects.

To temporarily resolve the issue, this is what worked for me.

  1. close the solution
  2. close VS 2022
  3. navigate to the project folder in File Explorer and show hidden files/folders
  4. delete or rename the ".vs" folder
  5. open the solution, errors gone.

Hope this temporary fix assists, but we really do need a permanent solution.

@marcin-przywoski
Copy link

@Brentec-ca we're not talking about Visual Studio here but rather Visual Studio code but I know what you are talking about. When I was inserting one custom control it didn't want to build this in znak designer but cleaning .vs folder, running dotnet clean and then rebuild fixes the issue

@PuR0kY
Copy link

PuR0kY commented Mar 22, 2024

Guys, delete "obj" folder of your application. Then rebuild, this will fix that. Visual Studio fix tho.

@marcin-przywoski
Copy link

@PuR0kY as you may have noticed we are talking about vs code extension here. What you're mentioning in VS is a completely separate thing

@PuR0kY
Copy link

PuR0kY commented Mar 22, 2024

@marcin-przywoski Yeah surely, I just had this issue with Visual Studio too, so thought would be nice to mention here. If not. I can delete it no without any problem.

@DustinLuck
Copy link

I'm seeing the same issue with

  • VSCode v1.87.2
  • C# v2.22.5
  • C# Dev Kit v1.4.29

The strange thing is that this isn't preventing me from running my application.

@jere344
Copy link

jere344 commented Apr 5, 2024

Same here over multiple projects all working fine. A way to hide the error would be good too.

@marcin-przywoski
Copy link

Navigate to the g.i.cs file of the offending Page or Window, delete the g.i.cs file and rebuild solution. Or, if you're lazy, delete the obj directory inside your project directory and rebuild. This fixed it for me.

We're not talking about visual studio, we're talking about vs code

@obnews
Copy link

obnews commented Aug 5, 2024

Guys, delete "obj" folder of your application. Then rebuild, this will fix that. Visual Studio fix tho.

It works on my VS, thanks.

@codenotworking
Copy link

I have the same issue with VS Code. As soon as I ran dotnet new wpf, open the project folder in VS Code, within MainWindows.xaml.cs I get The name 'InitializeComponent' does not exist in the current context (CS0103).

But if I perform dotnet run, it compiles properly without any errors, and the window appears.

@aianlinb
Copy link

aianlinb commented Aug 10, 2024

To all the people suffering from this problem:
I found a temporary solution that makes those errors disappear.
Just add these to your .csproj file:

<ItemGroup>
	<Compile Include="obj/Debug/net8.0-windows/*.cs">
		<AutoGen>True</AutoGen>
	</Compile>
</ItemGroup>

Edit:

The path should be where the MainWindow.g.cs (or something similar) is. (Find by yourself under obj, must build the project first)
And make sure your class declaration in MainWindow.xaml.cs matches the one in the generated file. (e.g. public partial class MainWindow : Window)
Finally, try restarting vscode and rebuilding the solution.

(If that doesn't work, maybe you can try replacing Include with Update.)

@marcin-przywoski
Copy link

@aianlinb didn't work for me :/ even after I changed the path in Include to the matching one in my project

@codenotworking
Copy link

To all the people suffering from this problem: I found a temporary solution that makes those errors disappear. Just add these to your .csproj file:

<ItemGroup>
	<Compile Include="obj/x64/Debug/net8.0-windows/*.cs">
		<AutoGen>True</AutoGen>
	</Compile>
</ItemGroup>

Unfortunately this didn't work for me.

@aianlinb
Copy link

@marcin-przywoski @codenotworking I edited some messages above, maybe try it again. wish good luck

@truncle
Copy link

truncle commented Aug 12, 2024

To all the people suffering from this problem: I found a temporary solution that makes those errors disappear. Just add these to your .csproj file:

<ItemGroup>
	<Compile Include="obj/Debug/net8.0-windows/*.cs">
		<AutoGen>True</AutoGen>
	</Compile>
</ItemGroup>

Edit:

The path should be where the MainWindow.g.cs (or something similar) is. (Find by yourself under obj, must build the project first) And make sure your class declaration in MainWindow.xaml.cs matches the one in the generated file. (e.g. public partial class MainWindow : Window) Finally, try restarting vscode and rebuilding the solution.

(If that doesn't work, maybe you can try replacing Include with Update.)

This works for me, it helps bro, thanks. Seems like Visual Studio does something for the generated build, but the vscode extension does not, so it should be done manually.

@Matt-Youlend
Copy link

Turn this on and it will be fixed. At least we now know it's the c# dev kit extension that is plaguing us.

image

@JoeRobich
Copy link
Member

The DevKit tracking issue for this bug is microsoft/vscode-dotnettools#1018

@mart1058
Copy link

Turn this on and it will be fixed. At least we now know it's the c# dev kit extension that is plaguing us.

image

Finally a solution that works!

@3boud0
Copy link

3boud0 commented Dec 29, 2024

Turn this on and it will be fixed. At least we now know it's the c# dev kit extension that is plaguing us.

image

Thank you a lot, it worked for me

@ShortArrow
Copy link

ShortArrow commented Jan 13, 2025

Turn this on and it will be fixed. At least we now know it's the c# dev kit extension that is plaguing us.

image

But, This make xUnit Test Explorer is disable.
This is a serious problem.

This is fine because you can run it with dotnet cli.
But even so, It's sad that after all the hard work and effort put into releasing this software, people can't use all of it just because InitializeComponent and some xaml component isn't recognized.

BTW, is there no similar bug in MAUI?
If so, it may be time to move in a new direction.

@ShortArrow
Copy link

ShortArrow commented Jan 13, 2025

To all the people suffering from this problem: I found a temporary solution that makes those errors disappear. Just add these to your .csproj file:

<ItemGroup>
	<Compile Include="obj/Debug/net8.0-windows/*.cs">
		<AutoGen>True</AutoGen>
	</Compile>
</ItemGroup>

Edit:

The path should be where the MainWindow.g.cs (or something similar) is. (Find by yourself under obj, must build the project first) And make sure your class declaration in MainWindow.xaml.cs matches the one in the generated file. (e.g. public partial class MainWindow : Window) Finally, try restarting vscode and rebuilding the solution.

(If that doesn't work, maybe you can try replacing Include with Update.)

This is work for me in my solution, that using wpf. Not real, this is fail.

  <ItemGroup>
    <Compile Update="obj/Debug/net8.0-windows/*.cs">
      <AutoGen>True</AutoGen>
    </Compile>
    <Compile Update="obj/Win32/Debug/net8.0-windows/*.cs">
      <AutoGen>True</AutoGen>
    </Compile>
  </ItemGroup>

@ahall-veic
Copy link

I unchecked the Dotnet: Prefer CSharp Extension, but it appears to have also disabled the build button in the IDE. I can still run the app from the command line via dotnet run, but it's not as convenient. I can definitely live with doing this rather than having this error and others like it in the editor, but it would be nice if the C# Dev Kit worked as expected.

@Foadsf
Copy link

Foadsf commented Apr 16, 2025

Confirming that adding "dotnet.preferCSharpExtension": true to the workspace .vscode/settings.json does work around the issue where InitializeComponent is incorrectly flagged as an error in the VS Code editor for WPF projects, even when dotnet build succeeds.

However, this feels like a temporary remedy rather than a proper solution. Relying on this setting effectively disables the primary C# Dev Kit (ms-dotnettools.csdevkit) language server features by forcing a fallback to the older C# extension logic (as indicated by warning messages like "The C# Dev Kit extension is disabled because the setting 'dotnet.preferCSharpExtension' is set to 'true'").

Ideally, the C# Dev Kit language server should be able to reliably locate and parse the generated .g.cs files (including the one defining InitializeComponent) produced by a standard WPF build using the correct .NET SDK. A more robust solution might involve:

  1. Ensuring C# Dev Kit correctly identifies the SDK specified by global.json for its analysis.
  2. Providing a way for C# Dev Kit to respect SDK discovery hints like dotnetAcquisitionExtension.existingDotnetPath.
  3. Offering a specific C# Dev Kit setting to explicitly define the dotnet.exe or SDK path it should use for project analysis, separate from build/run tasks.

This would allow developers to use the full capabilities of the C# Dev Kit while still getting correct IntelliSense for WPF projects.

@dibarbet
Copy link
Member

However, this feels like a temporary remedy rather than a proper solution. Relying on this setting effectively disables the primary C# Dev Kit

The C# Dev Kit side issue is tracked here - microsoft/vscode-dotnettools#1018
However the fix looks like it goes into the .NET SDK. This should definitely be fixed in .NET 10, and may be backported (see dotnet/wpf#10489)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests