In Elemish.WPF, can a parameter be sent when modal window is closed?

75 Views Asked by At

I have a MainWindow which opens a modal window (following the NewWindow example in the Elmish.WPF tutorial). My modal window deals almost exclusively with visual elements in xaml and C# code (i.e., handwriting recognition). Aside from creating a separate "submit" button in the xaml, I would like the closing window event to automatically send the accumalated data to the F# supporting code. This is what I've tried, but the closing/closed event is never triggered to send the data. How can this be done?

Thanks for any suggestions.

XAML:

<i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <i:InvokeCommandAction PassEventArgsToCommand="True" Command="{Binding ClosingCommand}" CommandParameter="{Binding ElementName=ProgressNote, Path=WordDictionary}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

Elmish.Wpf:

 "ClosingCommand" |> Binding.cmdParam paramWordDictionaryMsg

"ProgressNoteWindow"  |> Binding.subModelWin(                          
              (fun m -> m.ProgressNoteWin |> WindowState.ofOption),
                    snd,                                           
                    ProgressNoteWinMsg,                            
                    ProgressNoteWinBindings,                       
                    createProgressNote,                              
                    onCloseRequested = ProgressNoteWinCloseRequested,
                    isModal = true)  


type Msg = | ProgressNoteWinCloseRequested




let update msg m =
| ProgressNoteWinCloseRequested -> 
            match m.ProgressNoteWin with
            | Some { ConfirmState = Some ConfirmStateEnum.CloseRequested } -> { m with ProgressNoteWin = None }, Cmd.none
            | Some progressNoteWin -> { m with ProgressNoteWin = Some { progressNoteWin with ConfirmState = Some ConfirmStateEnum.CloseRequested } }, Cmd.none
            | None -> m, Cmd.none
1

There are 1 best solutions below

7
On BEST ANSWER

I really just wanted to post a comment to help you along, but it got far too long. This isn't a quality answer, but so be it. I think it'll solve your problem.

Forget the closing/closed events. It's simpler than that.

It is not clear to me what you want. What do you mean with "Aside from..."? I see two possibilities.

If you want the data to be submitted only when some button is clicked, then you should bind that click event to a message that you handle, and submit the data there, then perhaps you want to close the window which I believe you do by posting ProgressNoteWinCloseRequested. Probably indirectly, by posting a child message which the parent will listen to, before posting ProgressNoteWinCloseRequested to itself.

If instead you mean you don't want such a button, and instead you want to save data no matter which manner you close the window, then your ProgressNoteWinCloseRequested message handler in the parent model can take care of saving the data in the child model, which it has available before you set the state to WindowState.Closed.

Or maybe you can come up with something even more elegant inspired by this.

I'm not sure what the rest of your code might look like, but you'll probably figure out possible problems I can't see.

Also, be aware there's v 3.x and 4.x of Elmish.WPF, and I don't know if the problems I encountered when working with this machinery is still present in the latest 3.x, or have been fixed. Search in the GitHub repo of Elmish.WPF for information. Or easier - just do what I do and stick to the latest 4.x which is a prerelease on NuGet. Yes, it's a prerelease, but still more trustworthy in my opinion.