Skip to content
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

[BUG] Error referencing InitializeComponent in public App() constructor, but Build succeeded. #1018

Open
AleksandarDevic opened this issue Mar 22, 2024 · 15 comments
Assignees
Labels
area-maui bug Something isn't working triaged The issue has been triaged vs-sync Add this label to synchronize the issue to AzDo vs-syncd Linked to AzDO
Milestone

Comments

@AleksandarDevic
Copy link

AleksandarDevic commented Mar 22, 2024

Describe the Issue

When I create a simple MAUI project, in file MauiApp1\Platforms\Windows\App.xaml.cs
for method InitializeComponent I get a compiler error: 'App' does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)CS1061.

But when I do dotnet build I get info Build Succeeded!

image

Steps To Reproduce

  1. I created a simple MAUI project.

  2. In file MauiApp1\Platforms\Windows\App.xaml.cs, for method InitializeComponent I get compiler error:
    'App' does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type 'App' could be found (are you missing a using directive or an assembly reference?)CS1061.

  3. But when I do dotnet build I get info Build Succeeded!

Expected Behavior

The compiler should recognize the method InitializeComponent from MauiWinUIApplication class.

Environment Information

  • OS: Windows 11
  • VSCode: 1.87.2
  • .NET MAUI v0.9.7, C# v2.22.5, C# Dev Kit v1.4.29
    | VS Bug 2032244
@AleksandarDevic AleksandarDevic added the bug Something isn't working label Mar 22, 2024
@mauroa
Copy link
Member

mauroa commented Mar 25, 2024

Hi @AleksandarDevic. In the provided screenshot, I don't see the solution explorer at the left. I can only see your MAUI project folder opened as a workspace.
Do you have the C#, C# DevKit and MAUI DevKit extensions installed?
If so, could you please share the content of the C#, C# DevKit and .NET MAUI output panes?
Thanks

@mauroa mauroa added needs-more-info Needing more information, waiting on OP triaged The issue has been triaged labels Mar 25, 2024
@microsoft microsoft deleted a comment from github-actions bot Mar 27, 2024
@AleksandarDevic
Copy link
Author

AleksandarDevic commented Mar 27, 2024

Yes, I have all the extensions and their versions you can see in the first message.
Screenshoots:

  1. Output .NET MAUI
    image
    2.Output C# Dev Kit
    image
    3.Output C#
    image
    4.Problems:
    image

@github-actions github-actions bot removed the needs-more-info Needing more information, waiting on OP label Mar 29, 2024
Copy link

The needs-more-info label has been removed since the original bug filer has responded since the label was originally set.

@mauroa mauroa added the vs-sync Add this label to synchronize the issue to AzDo label Apr 12, 2024
@AllenD-MSFT AllenD-MSFT added the vs-syncd Linked to AzDO label Apr 12, 2024
@mauroa mauroa added this to the Post GA milestone Apr 25, 2024
@fushihara
Copy link

I am experiencing the same event.
dotnet new wpf.
I just opened a new project created with this command and now I get the same error.
It does not affect the build, but it is not very good mentally.

windows 10, dotnet 8.0.204.
Non wpf cli projects are fine.
Visual studio not vscode is fine.

@arunchndr arunchndr modified the milestones: Post GA, Backlog Jul 10, 2024
@AleksandarDevic
Copy link
Author

AleksandarDevic commented Jul 21, 2024

Hi, @mauroa , any info, because the same thing on the latest .Net9?

@jmlane
Copy link

jmlane commented Aug 8, 2024

It also looks like this was an ongoing issue with the previous OmniSharp C# extension as well.

@dibarbet
Copy link
Member

dibarbet commented Sep 5, 2024

Pretty straightforward to repro with dotnet new wpf and open in VSCode with devkit enabled. Will get this error
image

Works fine with C# extension standalone.

@tmeschter / @lifengl - IIRC what fixed this for the C# ext standalone was ensuring the DesignTimeMarkupCompilation target ran if available in the design time build. Not sure if that is applicable to devkit.
FYI there is a good amount of upvotes on this issue here - dotnet/vscode-csharp#5958

@testov1ch
Copy link

I still have the same problems. Have you already found a solution?

@BDRtest1234
Copy link

The same to you ┭┮﹏┭┮

@BDRtest1234
Copy link

BDRtest1234 commented Sep 25, 2024

image
There is a way to fix the problem temporarily.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>ControlLearn</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;ARM64</Platforms>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 8">win-x86;win-x64;win-arm64</RuntimeIdentifiers>
    <RuntimeIdentifiers Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win-$(Platform).pubxml</PublishProfile>
+      <ImplicitUsings>enable</ImplicitUsings>
+      <Nullable>enable</Nullable>
+      <SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
+      <WindowsSdkPackageVersion>10.0.19041.38</WindowsSdkPackageVersion>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="Assets\SplashScreen.scale-200.png" />
    <Content Include="Assets\LockScreenLogo.scale-200.png" />
    <Content Include="Assets\Square150x150Logo.scale-200.png" />
    <Content Include="Assets\Square44x44Logo.scale-200.png" />
    <Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
    <Content Include="Assets\StoreLogo.png" />
    <Content Include="Assets\Wide310x150Logo.scale-200.png" />
  </ItemGroup>

  <ItemGroup>
+    <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
+    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240829007" />
+    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>

  <!--
    Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
    Tools extension to be activated for this project even if the Windows App SDK Nuget
    package has not yet been restored.
  -->
  <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
    <ProjectCapability Include="Msix"/>
  </ItemGroup>

  <!--
    Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
    Explorer "Package and Publish" context menu entry to be enabled for this project even if
    the Windows App SDK Nuget package has not yet been restored.
  -->
  <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
    <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
  </PropertyGroup>
</Project>

you have to make sure that your PackageReference and the <SupportedOSPlatformVersion>&<WindowsSdkPackageVersion> are added

@pundang
Copy link

pundang commented Nov 10, 2024

is there any info about this? this problem is very annoying and i would like to use Omnisharp instead of the C# extension

@mrkmg
Copy link

mrkmg commented Jan 26, 2025

Writing in WPF on VSCode has extra errors with methods like "InitializeComponent" being marked as not existing. Would love to see a fix to this.

@PhoenixChenLu
Copy link

Extensions->C# Dev Kit->Disable->Restart Extensions->Enable
Done

@ridingmower
Copy link

I am also getting the same error with .net 9, I use the community tool kit

@JoeRobich
Copy link
Member

JoeRobich commented Feb 21, 2025

@lifengl Looks like WPF only hooks up Markup compilation if BuildingInsideVisualStudio is true. https://github.com/davidwengier/wpf/blob/98664b09434d4f48fb53cccb79d129faf2b20b93/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFx.targets#L99

Opened dotnet/wpf#10489

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-maui bug Something isn't working triaged The issue has been triaged vs-sync Add this label to synchronize the issue to AzDo vs-syncd Linked to AzDO
Projects
None yet
Development

No branches or pull requests