Skip to content

Latest commit

 

History

History
68 lines (48 loc) · 2.04 KB

8f3d768266935c.md

File metadata and controls

68 lines (48 loc) · 2.04 KB
title emoji type topics published
【.NET 5, F#】WinForms の使用方法
🖥
tech
dotnet
fsharp
winforms
true

前提

  • .NET CLI を使用する。
  • .NET CLI の winforms テンプレートは F# に対応していない1

手順

プロジェクトを作成する

dotnet new console -lang "F#" -o 場所\プロジェクト名

.fsproj を変更する

:::message OutputType の値は Exe でも動作しますが、WinForms アプリでは WinExe が標準です2。 :::

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

    <PropertyGroup>
+     <OutputType>WinExe</OutputType>
-     <OutputType>Exe</OutputType>
+     <TargetFramework>net5.0-windows</TargetFramework>
-     <TargetFramework>net5.0</TargetFramework>
      <RootNamespace>プロジェクト名</RootNamespace>
+     <UseWindowsForms>true</UseWindowsForms>
      <WarnOn>3390;$(WarnOn)</WarnOn>
    </PropertyGroup>

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

  </Project>

動作の確認

Program.fs を変更する

System.Windows.Forms.Application.Run(new System.Windows.Forms.Form())

プロジェクトを実行する

問題が無ければ空のウィンドウが表示されます。

dotnet run --project 場所\プロジェクト名.fsproj

end

Footnotes

  1. https://docs.microsoft.com/ja-jp/dotnet/core/tools/dotnet-new#:~:text=Windows%20%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%20(WinForms)%20%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3,%5BC%23%5D%E3%80%81VB

  2. https://docs.microsoft.com/ja-jp/dotnet/core/compatibility/sdk/5.0/automatically-infer-winexe-output-type#:~:text=Windows%20%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%20%E3%82%A2%E3%83%97%E3%83%AA%E3%81%A7%E3%81%AF%E3%80%81%E8%87%AA%E5%8B%95%E7%9A%84%E3%81%AB%20WinExe%20%E3%81%AB%E8%A8%AD%E5%AE%9A%E3%81%95%E3%82%8C%E3%81%BE%E3%81%99%E3%80%82