使用SDK 6.0中的MSBuild而不是Rider中捆绑的MSBuild构建WPF应用程序失败,并显示“程序不包含静态'Main'方法”

o2rvlv0m  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(206)

我正在使用JetBrains Rider开发WPF应用程序。它在IDE中生成和执行时不会出现任何问题,也不会在IDE中发布生成后从可执行文件中生成和执行时出现任何问题。但是,我需要将应用程序发布为单个文件。我尝试使用以下命令:
dotnet publish -c Release -p:PublishSingleFile=true --self-contained -p:PublishTrimmed=true
当我使用捆绑的MSBuild时,它创建一个没有所有依赖的.dll文件和其他资源的可执行文件。生成的可执行文件大小仅为111 KB。
我下载了SDK 6.0并更改了MSBuild版本。我还向.csproj添加了以下属性,以正确检测架构:

<GenerateResourceMSBuildArchitecture>CurrentArchitecture</GenerateResourceMSBuildArchitecture>
<GenerateResourceMSBuildRuntime>CurrentRuntime</GenerateResourceMSBuildRuntime>

在进行这些更改后,我的项目停止了构建,并遇到了以下错误:

Program does not contain a static 'Main' method suitable for an entry point
  MainWindow.xaml.cs(12, 13): [CS0103] The name 'InitializeComponent' does not exist in the current context
  App.xaml.cs(41, 24): [CS1061] 'MainWindow' does not contain a definition for 'DataContext' and no accessible extension method 'DataContext' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)
  App.xaml.cs(42, 24): [CS1061] 'MainWindow' does not contain a definition for 'Show' and no accessible extension method 'Show' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?)

我已经尝试了各种解决方案来解决这些错误,包括:

  • App.xaml的构建操作设置为ApplicationDefinition(最初设置)
  • 确保.csproj文件中存在以下ItemGroup(最初也存在):
<ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
  • UseWpf属性添加到.csproj文件并将其设置为true

不幸的是,这些解决方案都没有帮助我解决错误。我将感谢任何关于如何解决这个问题的指导或建议。

更新

.csproj文件

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x64</Platform>
    <UseWPF>true</UseWPF>
    <ProjectGuid>{272EC959-9B24-4026-A24E-4C0158D2C0C1}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <RootNamespace>WPFApp</RootNamespace>
    <AssemblyName>WPFApp</AssemblyName>
    <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <WarningLevel>4</WarningLevel>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <ApplicationIcon>Resources\app_icon.ico</ApplicationIcon>
    <LangVersion>7.3</LangVersion>
    <GenerateResourceMSBuildArchitecture>CurrentArchitecture</GenerateResourceMSBuildArchitecture>
    <GenerateResourceMSBuildRuntime>CurrentRuntime</GenerateResourceMSBuildRuntime>
    <StartupObject>WPFApp.App</StartupObject>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <StartupObject>WPFApp.App</StartupObject>
    <PlatformTarget>x64</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <StartupObject>WPFApp.App</StartupObject>
    <PlatformTarget>x64</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <ItemGroup>
    <Reference ...
  </ItemGroup>
...
  <ItemGroup>
    <Page Include="View\MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>
    <Page Include="View\PropertiesWindow.xaml" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Commands\" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
fiei3ece

fiei3ece1#

单文件发布要求标识目标平台。如果您现在没有这个,请尝试添加-p:RuntimeIdentifier=win-x64和/或-p:PlatformTarget=x64,替换您所针对的任何平台的正确值。作为参考,这里是我的一个项目中的工作项目文件,它做了同样的事情(在这个例子中是x86)。

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>annotations</Nullable>
    <UseWPF>true</UseWPF>
    <StartupObject>MyApp.App</StartupObject>
    <ApplicationIcon>Resources\App.ico</ApplicationIcon>
    <PlatformTarget>x86</PlatformTarget>
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
    <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
    <Platforms>AnyCPU;x86</Platforms>
    <AssemblyVersion>1.0.2.0</AssemblyVersion>
    <Version>1.0.2</Version>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>embedded</DebugType>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
    <DebugType>embedded</DebugType>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DebugType>embedded</DebugType>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
    <DebugType>embedded</DebugType>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="Resources\App.ico" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="7.0.0" />
    <PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup>
    <Resource Include="Resources\filex16.png" />
  </ItemGroup>

  <ItemGroup>
    <SplashScreen Include="Resources\splash.png" />
  </ItemGroup>

  <ItemGroup>
    <Compile Update="Properties\Settings.Designer.cs">
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <None Update="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
  </ItemGroup>

</Project>

相关问题