Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# dispatcher.invoke and delegate method

Tags:

wpf

f#

dispatcher

I looked for a solution to this problem for a long time without success.

I am porting some of my C# code to F# and I am struggling with a Dispatcher.Invoke for a WPF element. Since I am a total noob in F#, the only thing that I am sure of is that the problem is located between the chair and the keyboard.

Here is my C# code:

foreach (var k  in ChartList.Keys)            
        {
            ChartList[k].Dispatcher.Invoke(
              System.Windows.Threading.DispatcherPriority.Normal,
              new Action(
                delegate()
                {
                    ChartList[k].Width = area.Width / totalColsForm;
                    ChartList[k].Height = area.Height / totalRowsForm;
                    ChartList[k].Left = area.X + ChartList[k].Width * currentCol;
                    ChartList[k].Top = area.Y + ChartList[k].Height * currentRow;
                    ChartList[k].doShow();
                }
            ));
         }

The part I am struggling with is the new Action(delegate() ... ). The compiler did not like any of my attempts to translate it.

What would be the translation of this snippet in F#?

like image 301
Anass Avatar asked Mar 05 '13 15:03

Anass


1 Answers

The Invoke method has a couple of overloads, so if you have something not exactly right inside the action, you might have some weird errors because the type checker won't know which overload to call. I just tried this and it worked fine:

open System
open System.Windows.Controls
open System.Windows.Threading

let b = Button()
b.Dispatcher.Invoke(
    DispatcherPriority.Normal,
    Action(fun () -> 
        b.Content <- "foo"
    )) |> ignore
like image 141
Gustavo Guerra Avatar answered Oct 31 '22 12:10

Gustavo Guerra