WPF 应用程序使用新的 csproj 格式 [英] WPF App Using new csproj format

查看:14
本文介绍了WPF 应用程序使用新的 csproj 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am experimenting with migrating a WPF project, defined using the old csproj format, to the new format under VS 2017.

使用我在 如何将 Wpf 项目迁移到新的 VS2017 格式.

但我一直无法克服这个错误:

But I'm stuck at getting past this error:

错误 CS5001:程序不包含适合的静态主要"方法入口点

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

我的新式csproj文件如下:

My new-style csproj file is as follows:

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

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

  <ItemGroup>
    <EmbeddedResource Update="PropertiesResources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
    <Compile Update="PropertiesResources.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="....WPFUtilitiesJ4JUIJ4JUI.csproj" />
  </ItemGroup>

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

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

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

How do I configure the csproj file so that entry point gets built?

更新

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

Based on the tip about ApplicationDefinition I was able to get the project to compile. I could not set ApplicationDefinition in the BuildAction -- it was not one of the choices -- but had to edit the csproj file manually to include it. Here's the working version:

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

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

  <ItemGroup>
    <EmbeddedResource Update="PropertiesResources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
    <Compile Update="PropertiesResources.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="....WPFUtilitiesJ4JUIJ4JUI.csproj" />
  </ItemGroup>

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

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

还要注意指令上的排除过滤器.有必要阻止 MSBuild 尝试编译 App.xaml.cs 两次.

Note also the Exclude filter on the directive. It's necessary to keep MSBuild from attempting to compile App.xaml.cs twice.

推荐答案

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

You need to set the Build Action of App.xaml to ApplicationDefinition. The result is the following item in your csproj file:

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

这篇关于WPF 应用程序使用新的 csproj 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆