TrivialBehinds 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TrivialBehinds --version 1.0.0
NuGet\Install-Package TrivialBehinds -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TrivialBehinds" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TrivialBehinds --version 1.0.0
#r "nuget: TrivialBehinds, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TrivialBehinds as a Cake Addin
#addin nuget:?package=TrivialBehinds&version=1.0.0

// Install TrivialBehinds as a Cake Tool
#tool nuget:?package=TrivialBehinds&version=1.0.0

TrivialBehinds

A Micro Dependency Injector to make Windows Forms development in F# cheap and fun. Or C#.

Elevator pitch

  • You want to use the Visual Studio Forms Designer because hand writing the GUI creation code is messy.

  • You don't want to couple the GUI code with Forms class because the Form class should be mostly autogenerated. Logic, event binding code etc. lives elsewhere.

  • That elsewhere can be in another project, e.g. in an F# application that obviously needs to be compiled separately.

Use case stories

  • As an aspiring F# programmer, I have avoided doing Forms stuff because it's only properly supported in C# (with Designer)

  • As a succesfull but aging C# programmer, I don't want to go down the rabbit hole of the more sophisticated, but boilerplate and abstraction ridden archituctures (Model-View-Presenter, MVVM, ...).

Both of these beautiful dreams can be realized with a tiny library, written expressly for this purpose.

Hybrid F# - C# project

  • Create solution with two projects: DesignerPart.csproj (a C# Forms application) and FsMain.fsproj (an F# console application).
  • Draw and test the UI in DesignerPart
  • In the end of Form1.Designer.cs (assuming you didn't bother to change the form class name), you will find these type declarations:
    //Form1.Designer.cs
    private System.Windows.Forms.Button btnPlus;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button btnMinus;

You need to copy-paste them to a new class within the DesignerPart project and make them public (I removed the full type qualifiers for readability):

// Form1Ui.cs
public class Form1Ui
{
    public Button btnPlus;
    public Label label1;
    public Button btnMinus;
}

(Note that I didn't make the names PascalCase. Deal with it. Think of it as symmetry with originals or something.)

Now, TrivialBehinds steps in. In Form1.cs, after InitializeComponent call in ctor, we'll add the last bit of C# code we need:

public Form1()
{
    InitializeComponent();
    // this is only thing you need to add to your form
    var d = TrivialBehinds.CreateForUi(new Form1Ui
    {
        btnMinus = btnMinus,
        btnPlus = btnPlus,
        label1 = label1
    });
    Deactivate += (o, e) => d.Dispose();
    // boilerplate ends
}

You'll note that VS autocomplete gives a pretty satisfying show when writing that Form1Ui initialization boilerplace.

What this code does is:

  • It copies the ui control references that were verbosely created within InitializeComponent to a neat new class that can be passed around.
  • it requests the instantiation of a "behind" class that corresponds to Form1Ui. This "behind" class will hook up the events and implements all the UI logic in F#. Note that the DesignerPart doesn't need dll or project reference to the "behind" part.

The F# side

Huh, now to wash away that dirty feeling of authoring C# by switching to F# side:

  • Create "New F# Console application" (there is no Forms option)
  • Add a project reference to DesignerPart (because you will find both Form1 and Form1Ui class there)
  • Implement the "Behind" part, which has all the real logic:
type Form1Behind(ui: Form1Ui) =
    let mutable counter = 0

    let showCount() =
        ui.label1.Text <- sprintf "Count is: %d" counter

    do
        ui.btnPlus.Click.Add <|
            fun _ ->
                counter <- counter+1
                showCount()
        ui.btnMinus.Click.Add <|
            fun _ ->
                counter <- counter-1
                showCount()
  • Modify the 'main' like so:
[<EntryPoint; STAThread>]
let main argv =
    TrivialBehinds.RegisterBehind<Form1Ui, Form1Behind>()
    use form = new Form1()
    Application.Run(form)
    0

That's it! Set the F# side as startup project and press f5 to debug to your heart's content.

If that extra terminal window on launch bothers you, change outputtype form Exe to WinExe in FsMain.fsproj:

<OutputType>WinExe</OutputType>

Extra notes

  • The library is not yet in nuget, I will push it after a feedback round.
  • Windows Forms apps have fuzzy font rendering on Windows 10, unless you do a trick and compile with .NET Framework 4.7. Don't even think about publishing a Windows Forms app without this.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 707 3/16/2019
1.1.0 878 7/16/2018
1.0.0 1,049 7/10/2018