如何为f#应用程序设置Windows资源管理器图标

时间:2019-10-28 21:02:23

标签: .net-core f#

如何使用.NET Core 3.0或3.1创建简单的f#netcoreapp。并在Windows资源管理器中查看时显示应用程序图标?

这就像是一个问题before,但该解决方案似乎不适用于使用.NET Core 3.0或3.1 sdks构建的应用程序。

我已经创建了一个资源文件,并尝试编译该应用程序并使用命令行添加图标资源:

fsc Program.fs --win32res:AppIcon.res --target:exe

在这种情况下,应用程序可以编译,但是我没有Windows资源管理器图标。

我还尝试使用dotnet cli和下面的fsproj文件进行构建和发布(dotnet发布ConsoleApp1.fsproj -c版本-自包含-r win7-x64 -o publish)

在这种情况下,项目无法编译,并且出现错误“错误MSB4018:“ CreateAppHost”任务意外失败“。

任何帮助将不胜感激!

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Win32Resource></Win32Resource>
  </PropertyGroup>
 
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OtherFlags>--win32res:AppIcon.res</OtherFlags>
  </PropertyGroup>

  <ItemGroup>
    <EmbeddedResource Include="Resources\rebalance.ico" />
    <Resource Include="AppIcon.res">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Resource>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>

3 个答案:

答案 0 :(得分:2)

看起来我能够找出问题所在。我回头查看了Visual Studio中的资源文件,发现文件似乎存在并且有问题?

enter image description here

我使用了“ C:\ Program Files(x86)\ Windows Kits \ 10 \ bin \ 10.0.18362.0 \ x64 \ rc.exe” / v AppIcon.rc来生成res文件。也许它损坏了?即使在这种状态下,我仍然能够在Visual Studio中查看ico。无论如何,我从res中删除了损坏的ico文件,然后通过Visual Studio手动将其重新添加。这次看来Visual Studio可以识别该文件。

enter image description here

我使用此res文件继续努力。我重新运行了命令行dotnet publish TestIconRes.fsproj -c Release --self-contained -r win7-x64 -o publish,这一次应用程序图标在Windows资源管理器中可见。

fsc Program.fs --win32res:AppIcon.res也可以。

最终的fsproj文件如下:

<Project Sdk="Microsoft.NET.Sdk">
  
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Win32Resource></Win32Resource>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OtherFlags>--win32res:AppIcon.res</OtherFlags>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>

答案 1 :(得分:1)

尝试一下(在项目文件夹中包含.ico文件):

  <PropertyGroup>
    <ApplicationIcon>rebalance.ico</ApplicationIcon>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="rebalance.ico">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

答案 2 :(得分:0)

我也发现了这个问题,并以Scott Hutchinson建议的解决方案在.NET Core中为PropertyGroup提出了一个问题。

https://github.com/dotnet/core/issues/3771