novotnyllc / MSBuildSdkExtras

Extra properties for MSBuild SDK projects
MIT License
348 stars 42 forks source link

The name 'InitializeComponent' does not exist in the current context #303

Closed icnocop closed 11 months ago

icnocop commented 11 months ago

Hi.

Thank you for MSBuild.Sdk.Extras.

I'm trying to migrate an old style project to the new SDK-style project using MSBuild.Sdk.Extras.

The project has XAML pages.

I'm getting errors like CS0103: The name 'InitializeComponent' does not exist in the current context.

Here's a fork of this repo with the test project added: https://github.com/icnocop/MSBuildSdkExtras

It requires https://marketplace.visualstudio.com/items?itemName=RamiAbughazaleh.SilverlightProjectSystem2022 to successfully build the SilverlightApplication project, which does not use MSBuild.Sdk.Extras.

The SilverlightApplicationExtras project was created which uses MSBuild.Sdk.Extras and this project has errors when trying to build.

1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\About.xaml.cs(20,13,20,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\MainPage.xaml.cs(20,13,20,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\Home.xaml.cs(20,13,20,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\App.xaml.cs(22,13,22,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\ErrorWindow.xaml.cs(11,13,11,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\ErrorWindow.xaml.cs(14,17,14,29): error CS0103: The name 'ErrorTextBox' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\ErrorWindow.xaml.cs(20,13,20,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\ErrorWindow.xaml.cs(23,17,23,29): error CS0103: The name 'ErrorTextBox' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\ErrorWindow.xaml.cs(29,13,29,32): error CS0103: The name 'InitializeComponent' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\Views\ErrorWindow.xaml.cs(30,13,30,25): error CS0103: The name 'ErrorTextBox' does not exist in the current context
1>E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\MainPage.xaml.cs(26,41,26,56): error CS0103: The name 'LinksStackPanel' does not exist in the current context

The projects pretty much have the same code except for the differences in the csproj file.

Any ideas?

Thank you.

icnocop commented 11 months ago

I was able to resolve those compiler errors by doing the following

  1. Change <Project Sdk="MSBuild.Sdk.Extras/3.0.44"> to <Project Sdk="Microsoft.NET.Sdk">
  2. Add <OutputPath>bin</OutputPath> within <PropertyGroup></PropertyGroup>
  3. Add <Import Project="$(MSBuildProgramFiles32)\MSBuild\Microsoft\Silverlight\v5.0\Microsoft.Silverlight.CSharp.targets" />

However, I get the following warning message:

C:\Program Files\dotnet\sdk\7.0.401\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(37,3): warning MSB4011: "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v5.0\Microsoft.Silverlight.CSharp.targets (20,5)". This is most likely a build authoring error. This subsequent import will be ignored. [E:\github\icnocop\MSBuildSdkExtras\Tests\SilverlightApplicationExtras\SilverlightApplicationExtras.csproj]

Is there a way to resolve the warning message?

Thank you.

icnocop commented 11 months ago

I was able to resolve it using the following:

<Project Sdk="MSBuild.Sdk.Extras/3.0.44">
  <PropertyGroup>
    <TargetFrameworks>sl5</TargetFrameworks>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>SilverlightApplicationExtras</RootNamespace>
    <AssemblyName>SilverlightApplicationExtras</AssemblyName>
    <TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
    <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
    <SilverlightApplication>true</SilverlightApplication>
    <SupportedCultures>
    </SupportedCultures>
    <XapOutputs>true</XapOutputs>
    <GenerateSilverlightManifest>true</GenerateSilverlightManifest>
    <XapFilename>SilverlightApplicationExtras.xap</XapFilename>
    <SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate>
    <SilverlightAppEntry>SilverlightApplicationExtras.App</SilverlightAppEntry>
    <TestPageFileName>SilverlightApplicationExtrasTestPage.html</TestPageFileName>
    <CreateTestPage>true</CreateTestPage>
    <ValidateXaml>true</ValidateXaml>
    <EnableOutOfBrowser>false</EnableOutOfBrowser>
    <OutOfBrowserSettingsFile>Properties\OutOfBrowserSettings.xml</OutOfBrowserSettingsFile>
    <UsePlatformExtensions>false</UsePlatformExtensions>
    <ThrowErrorsInValidation>true</ThrowErrorsInValidation>
    <LinkedServerProject>
    </LinkedServerProject>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <UseWpf>true</UseWpf>
    <ImportFrameworkWinFXTargets>true</ImportFrameworkWinFXTargets>
    <OutputPath>bin</OutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="mscorlib" />
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Windows" />
    <Reference Include="system" />
    <Reference Include="System.Core" />
    <Reference Include="System.Net" />
    <Reference Include="System.Xml" />
    <Reference Include="System.Windows.Browser" />
    <Reference Include="System.Windows.Controls" />
    <Reference Include="System.Windows.Controls.Navigation" />
  </ItemGroup>
  <ItemGroup>
    <None Include="Properties\AppManifest.xml" />
  </ItemGroup>
  <ItemGroup>
    <Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
  </ItemGroup>
  <Target Name="BeforeMarkupCompilePass1" BeforeTargets="MarkupCompilePass1">
    <ItemGroup>
      <Page Include="**\*.xaml" Exclude="App.xaml;**\bin\**\*.xaml;**\obj\**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
    </ItemGroup>
  </Target>
  <Target Name="BeforeMainResourcesGeneration" BeforeTargets="MainResourcesGeneration">
    <RemoveDuplicates Inputs="@(MainEmbeddedFiles)">
      <Output
          TaskParameter="Filtered"
          ItemName="UniqueMainEmbeddedFiles"/>
    </RemoveDuplicates>
    <ItemGroup>
      <MainEmbeddedFiles Remove="@(MainEmbeddedFiles)" />
      <MainEmbeddedFiles Include="@(UniqueMainEmbeddedFiles)" />
    </ItemGroup>
  </Target>
</Project>