F # MVVM event using WPF

I've been working with Event MVVM for a couple of weeks (first time using the design pattern in F #), and I love the idea of โ€‹โ€‹separating the view and model, and a "functional" controller. But when viewing the book in WPF it seems to me that it would be easier if I could dispatch events directly. Also in some situations I need to keep control of the code.

More specific:

  • How to close window defined as usercontrol in XAML file
  • There seems to be less need for buttons (triggering boolean states that hold state), with a more automated feel as a result if I could address events directly

Does anyone share this experience or am I still missing something? Is it advisable to go back to FsXaml or polyglot MVVM?

+3


source to share


1 answer


It turns out that the code is actually very extensible. Based on demos I managed to turn the textbox into a numeric one. The code that does this is very simple, but I wanted to define a custom event accessory. What can be done:

Extend UserControl.xaml header with

xmlns:fsxaml="http://github.com/fsprojects/FsXaml"   
fsxaml:ViewController.Custom="{x:Type views:CompositionUserControl}"

      

And replace the original code in UserControl.xaml.fs:

namespace Space.Views

open FsXaml

type UserView = XAML<"View/UserControl.xaml", true>

type CompositionUserControl () =

    member __.ViewModel = Space.ViewModels.UserControlViewModel(Space.Models.Handling.proces)

      

from

namespace Space.Views

open FsXaml
open System

type UserView = XAML<"View/UserControl.xaml", true>

type CompositionUserControl () =
    inherit UserControlViewController<UserView>()

    let numeric (txt : string) =
        try txt |> int with
        | :? System.FormatException -> 0
        | _ -> 1

    override this.OnLoaded view = 
        view.Box.PreviewTextInput.Add(fun e -> if numeric e.Text = 0 then e.Handled <- true) 

    member __.ViewModel = Space.ViewModels.UserControlViewModel(Space.Models.Handling.proces)

      



EDIT

Looking back on this post, here's my progress regarding my initial questions:

How to close window defined as usercontrol in XAML file

Using the attached property DialogCloser .

There seems to be less need for buttons (triggering booleans that hold state), with a more automated feeling as a result if I could directly access events

The key point here is to learn:

  • How to truly separate the view (model) from the model
  • How to use XAML to use its full power.
+4


source







All Articles