elmish / Elmish.WPF

Static WPF views for elmish programs
Other
428 stars 69 forks source link

How to open the WPF window a second time #210

Open ScottHutchinson opened 4 years ago

ScottHutchinson commented 4 years ago

In my application, another project calls the LoadWindow function shown below. But after the user closes the MsgTypeFiltersWindow window and attempts to open it again by calling the LoadWindow function again, I get 'System.InvalidOperationException' in PresentationFramework.dll The Application object is being shut down..

How can I write the LoadWindow function so it can be called over and over again?

Thanks

module PublicAPI =
    open NG_DART_WPF

    let LoadWindow (msgTypeID: int) (msgTypeName: string) (parentStructName: string) =
      Program.mkSimpleWpf App.init App.update App.rootBindings
      |> Program.withConsoleTrace
      |> Program.runWindowWithConfig
        { ElmConfig.Default with LogConsole = true; Measure = true }
        (MsgTypeFiltersWindow())
ScottHutchinson commented 4 years ago

Yeah, I agree. For completeness, we might want one sample that does not use Elmish for the main application, and one that does use it.

Also, since the Program.startElmishLoop is public, I can also do it like below without modifying Elmish.WPF at all. We could still add sample(s) showing how to do this. It seems obvious and trivial now, but it took me a couple days of frustration before I figured it out.

        let showDialogWithConfig config (window: Window) program =
            Program.startElmishLoop config window program
            window.ShowDialog ()

        Program.mkSimpleWpf init App.update App.rootBindings
        |> showDialogWithConfig
            ElmConfig.Default
            (MsgTypeFiltersWindow())
        |> ignore
TysonMN commented 4 years ago

Since Program.startElmishLoop is already public, I think it is not worth adding another public function to also show a Window as a dailog.

cmeeren commented 4 years ago

Brilliant! Let's just focus on a sample, then.

Personally I think it's sufficient to show a non-Elmish app using Elmish.WPF for sub-windows. Using separate Elmish dispatch loops for an app that already runs Elmish.WPF at the root seems unnecessary and, while possible, not something that anyone would really want to do. Feel free to enlighten me with counter-examples, though.