F # dispatcher.invoke and delegation method

I have been looking for a solution to this problem for a long time without success.

I am porting part of my C # code to F # and I am struggling with Dispatcher.Invoke for a WPF element. Since I'm a complete noob in F #, the only thing I'm sure is that the problem is 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'm struggling with is the new action (delegate () ...). The compiler did not like any of my attempts to translate it.

What will be the translation of this fragment in F #?

+6
source share
1 answer

The Invoke method has a couple of overloads, so if you have something not quite right inside the action, you may have some strange errors, because the type checker does not know which overload needs to be called. I just tried this and it worked perfectly:

 open System open System.Windows.Controls open System.Windows.Threading let b = Button() b.Dispatcher.Invoke( DispatcherPriority.Normal, Action(fun () -> b.Content <- "foo" )) |> ignore 
+6
source

All Articles