How can I use things such as System.Windows.Threading in an F# project?

I’m trying to get some projects from the “Concurrency in .NET” book by Riccardo Terrell working but the downloaded projects cause so many build errors that are incomprehensible to me that I’m trying to create my own projects from scratch and copy the necessary code into them.

The code contains things such as:

open System.Windows
open System.Threading
open System.Windows.Threading
...
let form = Window(Content=image, Title="Game of Life", Width=800., Height=823.)
    let application = new Application()

…and:

open System.Windows
open System.Windows.Media
open System.Windows.Media.Imaging
open System.Threading
...
let createImage (grid:Grid) (pixels:Array) =
    BitmapSource.Create(grid.Width, grid.Height, 96., 96., PixelFormats.Gray8, null, pixels, grid.Width)

…but I don’t know what sort of project to create (I’m using Visual Studio), or which NuGet packages to use, or how to reference the correct things, or how to configure things to get the code working.

Can anyone help?

(I found a thread in the forums about using this sort of thing in a script but I don’t know if any of that is relevant to what I’m trying to do.)

I don’t have a great answer, but I looked up the System.Windows.Threading namespace and it started talking about a bunch of WPF stuff. I looked up WPF and found this guide on creating a new project. I don’t use Visual Studio, so I did a dotnet new list and saw


so no F# support :unamused:.
I can create a new C# project and look in the csproj file to see how it’s setup:
image

Hopefully it would work if you copied those same things into your F# project?

That said, I think WPF is a very C#-y way of handling UI. I haven’t built any UIs in F#, but I’ve heard good things about Fabulous, so that might be worth looking into. I’m guessing it’s a little bit smoother experience than trying to get WPF working on F#.

Thanks for looking into this.

I think I have managed, with your help, to get things working.

For anyone else who is having trouble, here’s what I did:

  1. I created a new F# Console App project;
  2. I copied the FS code files over to the new project;
  3. I edited the project Properties and changed “Output Type” from “Console Application” to “Windows Application”;
  4. I also set “Windows Presentation Foundation – Enable WPF for this project” to ON;
  5. I then saved the Properties;
  6. I then edited the Project File and added <TargetFramework>net7.0-windows</TargetFramework> to the Property Group, and saved the file.

The errors went away; I could then build the solution and run the application (after setting that project to be the start-up project of the solution).

I don’t know how much of the above is actually needed to get it working so one or more steps may not be necessary.

Here’s the complete Project File:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0</TargetFramework
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWPF>True</UseWPF>
  </PropertyGroup>

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

</Project>

(The stuff in the ItemGroup should be the FS files in the project, not just these specific items.)

Thanks again for your, as ever, valuable help.

P.S. I’ll try to remember looking at Fabulous later, it would be great to get the UI done in F# without resorting to C#, but at the moment I’m just trying to do the stuff in the book I’m reading.

1 Like