Search code examples
f#elmish-wpf

How does Elmish.WPF marshall secondary windows into the main function?


How do the various #Windows (mainWindow, createWindow1, and createWindow2) get marshalled as parameters when calling the Elmish.WPF NewWindow code sample Program.fs as seen here ...

let main mainWindow (createWindow1: Func<#Window>) (createWindow2: Func<#Window>) =
  let logger =
    LoggerConfiguration()
      .MinimumLevel.Override("Elmish.WPF.Update", Events.LogEventLevel.Verbose)
      .MinimumLevel.Override("Elmish.WPF.Bindings", Events.LogEventLevel.Verbose)
      .MinimumLevel.Override("Elmish.WPF.Performance", Events.LogEventLevel.Verbose)
      .WriteTo.Console()
      .CreateLogger()
  let createWindow1 () = createWindow1.Invoke()
  let createWindow2 () =
    let window = createWindow2.Invoke()
    window.Owner <- mainWindow
    window

  let init () = App.init
  let bindings = App.bindings createWindow1 createWindow2
  WpfProgram.mkSimple init App.update bindings
  |> WpfProgram.withLogger (new SerilogLoggerFactory(logger))
  |> WpfProgram.startElmishLoop mainWindow

???

What plumbing is happening in .NET, WPF, Elmish.WPF that organizes the reference found at the top of NewWindows.xaml to NewWindow.Core so that the F# function main is called with the windows marshalled and passed in in the right order?


Solution

  • As I finished the above question I determined what I think is the answer. Left the question in case it helps someone else. -RCHF

    For reference I have thoroughly read the the Elmish.WPF Tutorial.

    In arriving at this answer I found Getting started with Elmish.WPF, item #7 and #8 helpful.

    In Elmish.WPF, the MainWindow.xaml`` file calls the C# code-behind file MainWindow.xaml.cs*must* have aStartElmish``` function with the following code …

        private void StartElmish(object sender, EventArgs e)
        {
          this.Activated -= StartElmish;
          Program.main(MainWindow, () => new Window1(), () => new Window2());
        }
    

    Here you see references to all three windows in the Elmish.WPF NewWindow code sample (see C# XAML) and F# Core) being marshalled into the subordinate F# main function.