vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

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

Open HyVong007 opened this issue 1 year ago • 34 comments

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 !

HyVong007 avatar Jul 26 '23 05:07 HyVong007

same there

heartacker avatar Jul 26 '23 13:07 heartacker

@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?

JoeRobich avatar Jul 26 '23 17:07 JoeRobich

@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

HyVong007 avatar Jul 30 '23 06:07 HyVong007

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.

fbiwarning avatar Aug 01 '23 15:08 fbiwarning

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

nesteg avatar Sep 09 '23 12:09 nesteg

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

marcin-przywoski avatar Sep 09 '23 21:09 marcin-przywoski

same issue. Any solution?

Cryptome2 avatar Sep 20 '23 08:09 Cryptome2

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.

sauntheninja2 avatar Sep 25 '23 07:09 sauntheninja2

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

usamachohan053 avatar Oct 05 '23 21:10 usamachohan053

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

amine-aboufirass avatar Oct 18 '23 14:10 amine-aboufirass

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

Kokujou avatar Oct 19 '23 09:10 Kokujou

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

yingjiewx avatar Oct 23 '23 07:10 yingjiewx

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

bubuwang007 avatar Nov 03 '23 14:11 bubuwang007

I have the same.

vwnd avatar Nov 23 '23 13:11 vwnd

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?

mart1058 avatar Dec 19 '23 17:12 mart1058

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.

MelDommer avatar Jan 20 '24 16:01 MelDommer

Still broken, it has been months.

Matt-Youlend avatar Feb 23 '24 14:02 Matt-Youlend

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.

Brentec-ca avatar Feb 23 '24 21:02 Brentec-ca

@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

marcin-przywoski avatar Feb 24 '24 11:02 marcin-przywoski

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

PuR0kY avatar Mar 22 '24 06:03 PuR0kY

@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

marcin-przywoski avatar Mar 22 '24 09:03 marcin-przywoski

@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.

PuR0kY avatar Mar 22 '24 09:03 PuR0kY

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.

DustinLuck avatar Mar 31 '24 06:03 DustinLuck

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

jere344 avatar Apr 05 '24 18:04 jere344