gpt4 book ai didi

c# - WPF 应用程序使用新的 csproj 格式

转载 作者:可可西里 更新时间:2023-11-01 03:09:43 24 4
gpt4 key购买 nike

我正在尝试将使用旧 csproj 格式定义的 WPF 项目迁移到 VS 2017 下的新格式。

使用在 How-to migrate Wpf projects to the new VS2017 format 中找到的信息,我能够获得成功构建的大部分方法.

但我一直坚持克服这个错误:

error CS5001: Program does not contain a static 'Main' method suitable for an entry point

我的新式csproj文件如下:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
<OutputType>winexe</OutputType>
<TargetFramework>net47</TargetFramework>
<ApplicationIcon />
<OutputTypeEx>winexe</OutputTypeEx>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<Compile Update="Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />
<None Update="Settings.settings" LastGenOutput="Settings.Designer.cs" Generator="SettingsSingleFileGenerator" />

<Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" />
<Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />

<Resource Include="assets\*.*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.0" />
<PackageReference Include="Autofac.Extras.CommonServiceLocator" Version="4.0.0" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.0.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.0.8" />
<PackageReference Include="MaterialDesignColors" Version="1.1.3" />
<PackageReference Include="MaterialDesignThemes" Version="2.3.0.823" />
<PackageReference Include="MvvmLightLibs" Version="5.3.0" />
<PackageReference Include="Serilog" Version="2.4.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\WPFUtilities\J4JUI\J4JUI.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>

<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>

如何配置 csproj 文件以便构建入口点?

更新

根据有关 ApplicationDefinition 的提示,我能够编译项目。我无法在 BuildAction 中设置 ApplicationDefinition——这不是选择之一——但必须手动编辑 csproj 文件以包含它。这是工作版本:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
<OutputType>winexe</OutputType>
<TargetFramework>net47</TargetFramework>
<ApplicationIcon />
<OutputTypeEx>winexe</OutputTypeEx>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<Compile Update="Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />
<None Update="Settings.settings" LastGenOutput="Settings.Designer.cs" Generator="SettingsSingleFileGenerator" />

<Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" Exclude="App.xaml" />
<Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />

<Resource Include="assets\*.*" />

<ApplicationDefinition Include="App.xaml">
<Generator>MsBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>

</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.0" />
<PackageReference Include="Autofac.Extras.CommonServiceLocator" Version="4.0.0" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.0.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.0.8" />
<PackageReference Include="MaterialDesignColors" Version="1.1.3" />
<PackageReference Include="MaterialDesignThemes" Version="2.3.0.823" />
<PackageReference Include="MvvmLightLibs" Version="5.3.0" />
<PackageReference Include="Serilog" Version="2.4.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\WPFUtilities\J4JUI\J4JUI.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>

<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>

另请注意指令中的排除过滤器。有必要防止 MSBuild 尝试编译 App.xaml.cs 两次。

最佳答案

您需要将App.xaml 的Build Action 设置为ApplicationDefinition。结果是您的 csproj 文件中的以下项目:

<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>

关于c# - WPF 应用程序使用新的 csproj 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com