Archive

WPF

In this video, I discuss the DataTrigger within Xamarin.Forms.

Advertisements

I’m chopping it up with Ody about F#, Property-based Testing, Akka, etc.

Reactive Programming – 2:52
Akka dot Net – 23:26
Immutable Databases – 37:00
Architects + F# – 47:00
Multi-Threading Discussion – 52:22
Testing Discussion – 58:45
Codebases and Architecture – 1:17:23
Property based Testing – 1:32:33
F# vs C# for User Interfaces – 1:44:00
Motivation for Software Developers – 1:57:20
Is there any need for a Database? – 2:03:00
Architecture – 2:12:00

Intro

This post will provide insight on how to implement the DelegateCommand in F#.

The Delegate Command

The DelegateCommand implements the ICommand interface. This command implementation provides two delegates, Execute and CanExecute.

The following is an implementation of the DelegateCommand:

open System
open System.Windows
open System.Windows.Input
open System.ComponentModel

type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
    let event = new DelegateEvent<EventHandler>()
    interface ICommand with
        [<CLIEvent>]
        member this.CanExecuteChanged = event.Publish
        member this.CanExecute arg = canExecute(arg)
        member this.Execute arg = action(arg)

The following code is an example implementation of how the DelegateCommand is used:

member self.Play =
    DelegateCommand ((fun _ -> printf "Hello World"), fun _ -> true) :> ICommand

If you’ve noticed, in order to expose the Execute and CanExecute methods for a client to call, we cast the command to the interface type as shown below:

:> ICommand

Without the cast to ICommand, a client cannot invoke the command because it’s just not exposed within F# at the concrete implementation level.

The XAML would look something like this:

<Button Grid.Row="6" Grid.Column="1" Content="Go!" Command="{Binding Play}" />

Conclusion

In conclusion, I have provided insight on how to implement the DelegateCommand in F#. It is essential to cast an implementation of a DelegateCommand to an ICommand for callers to invoke it.

Intro

I am still plugging away on my F# implementation of the Game of Life. I am now at the point where I would like to see some visuals. As a result, I created a client project that consists of a view-model, a XAML file, and a value converter class.

Impediment

I am unable to launch the actual application.
I received the following warning:
“Main module of program is empty: nothing will happen when it is run”

I then added a new file and provided the following code:

module Bootstrap

open System.Windows

[<EntryPoint>]
let main args =
    Application.Current.Run() |> ignore;
    // Return 0. This indicates success.
    0

The code above still doesn’t resolve my issue though. Hence, I receive a null reference exception on Application.Current.

ViewModel

As of right now, the viewmodel class looks like the following:

namespace Client

open UILogic
open Model

type ViewModel() =
    inherit ViewModelBase()

    let rowCount = 3
    let grid = rowCount |> createGrid
                        |> setCell { X=1; Y=1; State=Alive }

    let mutable _cells = grid |> Map.toSeq
                              |> Seq.map snd
                              |> Seq.toList
    member this.Cells
        with get() = _cells 
        and set(value) =
            _cells <- value


XAML

The XAML file is hardcoded to reflect a fixed grid. Later on, I will generate a grid programmatically.

The XAML is as follows:

<Window x:Class="Client.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Client"
        Background="Black"
        Title="Game of Life" Height="450" Width="500">

    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>

    <Window.Resources>
        <local:StateToBrushConverter x:Key="StateToBrushConverter" />
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <Rectangle Grid.Row="0" Grid.Column="0" Fill="{Binding Cells[0], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="0" Grid.Column="1" Fill="{Binding Cells[1], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="0" Grid.Column="2" Fill="{Binding Cells[2], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="1" Grid.Column="0" Fill="{Binding Cells[3], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="1" Grid.Column="1" Fill="{Binding Cells[4], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="1" Grid.Column="2" Fill="{Binding Cells[5], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="2" Grid.Column="0" Fill="{Binding Cells[6], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="2" Grid.Column="1" Fill="{Binding Cells[7], Converter={StaticResource StateToBrushConverter}}" />
        <Rectangle Grid.Row="2" Grid.Column="2" Fill="{Binding Cells[8], Converter={StaticResource StateToBrushConverter}}" />
    </Grid>

</Window>

StateToBrushConverter

I need to differentiate living cells from dead cells. As a result, I added a value converter to display the color of living cells.

namespace Client
open System.Windows.Data
open Model
open System.Windows.Media
 
type StateToBrushConverter() =
    interface IValueConverter with
        member x.Convert(value, targetType, parameter, culture) = 
            let cell = value :?> Cell
            match cell.State with 
            | Alive -> SolidColorBrush(Colors.LightGreen) :> obj
            | Dead  -> SolidColorBrush(Colors.Black)      :> obj
 
        member x.ConvertBack(value, targetType, parameter, culture) = failwith "Not implemented yet"

Conclusion

I am still plugging away on my F# implementation of the Game of Life. I am now at the point where I would like to see some visuals. As a result, I created a client project that consists of a view-model, a XAML file, and a value converter class.