Can I use Microsoft.Graphics.Win2D in an F# project?

I’d like to use an F# project to draw onto a Win2D CanvasControl in a WinUI3 application.

The process would be something like:

  • User does something - presses a button, slides a slider, etc. - in the WinUI3 application;

  • This sends a ‘current state’ to a function in StateProject (F#);

  • Which modifies the state and returns a new ‘current state’ to the WinUI3 application;

  • Which then invalidates the CanvasComponent;

  • The Draw method of which sends the ‘current state’ (and some other stuff) to a function in DrawingProject (F#);

  • Which draws on the CanvasComponent.

The ‘current state’ will be boxed when passed between projects so that the WinUI3 application cannot modify or see the contents, and unboxed when in the F# projects (because the state contains F#-only things, e.g. DUs, Lists, etc.).

The overall reasoning is that the StateProject does all of the state set-up and modification, DrawingProject only does the drawing, and the WinUI3 application just uses the F# projects as necessary, removing most of the ‘work’ from the application itself.

I tried to add Microsoft.Graphics.Win2D as a package of DrawingProject but that project failed to compile with errors that I didn’t recognise or understand (it even had a problem with the ‘namespace’ keyword which was the first bit of text in the only .fs file in the project).

I’ve looked at the dependencies for Win2D and it’s dependent upon Microsoft.WindowsAppSDK which is itself dependent upon Microsoft.Web.WebView2 and Microsoft.Windows.SDK.BuildTools, which I think are a bit too much to add to a little F# project that just needs to draw some basic things.

Has anyone managed to get this sort of thing working and, if so, how?

Note: Whatever is done needs to be compatible with .NET between 6.0 and 8.0 (inclusive) because of WinUI3 restrictions.

I think you need learn docs and I recommend to see Elmish · Elmish for build UI.

Thanks but I can’t see how that would meet my requirements.

I should have said earlier that I need to draw procedurally generated shapes – rectangles, ellipses, polylines, etc. – which are created by some F# code.

And I am using Win2D to do that because I have a WinUi3 application.

And I have a WinUi3 application because I have no idea how to package an F# project for the Microsoft Store (or if that’s even possible).

I think I’ve managed to ‘smudge’ my requirements by returning a sequence of intermediate objects from the F# code to be used in the C# drawing code and I’m hoping that the overhead of doing so won’t affect the result too much (some quick tests look promising so far).