Search code examples
f#elmish-wpf

How to manage multiple windows, usercontrols, and customcontrols with Elmish.wpf and F#?


I am a newbie to F#. I have recently be introduced to Elmish.wpf and the MVU design. The application I am working with is in C# WPF with many WPF usercontrols, customcontrols, and windows. It appears that Elmish.wpf flattens the concept of viewmodels into a single datacontext. (??). Can Elmish.wpf be used with multiple windows, usercontrols, and customcontrols? ("Multiple" here means about 20 windows, usercontrols, and customcontrols.)

If so, is there an example of this?

In looking at the Elmish.wpf webside, it seems that all windows need to be created upon initialization--

let main mainWindow (createWindow1: Func<#Window>) (createWindow2: Func<#Window>) =
  let createWindow1 () = createWindow1.Invoke()
  let createWindow2 () =
    let window = createWindow2.Invoke()
    window.Owner <- mainWindow
    window
  let bindings = App.mainBindings createWindow1 createWindow2
  Program.mkSimpleWpf App.init App.update bindings
  |> Program.withConsoleTrace
  |> Program.runWindowWithConfig
    { ElmConfig.Default with LogConsole = true; Measure = true }
    mainWindow

Is there a better way to do this? Am I barking up the wrong tree?

Thanks in advance.


Solution

  • Yes, Elmish.WPF absolutely supports multiple windows and user-defined controls.

    Many of the samples in the Elmish.WPF repo demonstrate this one way or another. For example, the NewWindow sample demonstrates how to open new windows, and the SubModel (along with many others) sample demonstrates how to use custom UserControls. The SubModelSeq sample is currently the most complex one, demonstrating arbitrarily deep trees of recursive UserControls.

    All of this is also described in the official Elmish.WPF tutorial.

    (For future reference, this is the current commit at the time of writing; the samples may have changed since then.)