Czy istnieje przykład, jak wywołać DelegateCommand na viewmodel napisane w F #?Jak używać DelegateCommand na viewmodel?
znalazłem następujący kod do implementacji DelegateCommand here:
type DelegateCommand<a>(execute : 'a -> unit, ?canExecute : 'a -> bool) =
let event = Event<_,_>()
interface ICommand with
member this.CanExecute(param : obj) =
if canExecute.IsSome then
canExecute.Value(param :> 'a)
else true
member this.Execute (param : obj) =
execute(param :> 'a)
jednak nie jest nowy w F #, nie jestem pewien, czy powinienem używać tej implementacji lub jeśli istnieje jakaś inna podejście powinienem być dźwignia.
Oto test, który chciałbym wykonać kiedyś dowiedzieć się, jak wykonać DelegateCommand:
module CreateProfileViewModel.Tests
open FsUnit
open NUnit.Framework
open CreateProfile.UILogic
[<Test>]
let ``create profile``() =
// Setup
let viewModel = ViewModel()
viewModel.FirstName <- "Scott"
viewModel.LastName <- "Nimrod"
// Test
viewModel.Submit.Execute();
// Verify
let expected = false // TODO - implement some state-change
expected |> should equal true
Moje ViewModel jest następujący:
module CreateProfile
open Core.UILogic
type ViewModel() =
inherit ViewModelBase()
let mutable firstName = ""
let mutable lastName = ""
member this.FirstName
with get() = firstName
and set(value) =
firstName <- value
base.NotifyPropertyChanged(<@ this.FirstName @>)
member this.LastName
with get() = lastName
and set(value) =
lastName <- value
base.NotifyPropertyChanged(<@ this.LastName @>)
mam następujący model bazy danych:
module Core.UILogic
open System.ComponentModel
open Microsoft.FSharp.Quotations.Patterns
type ViewModelBase() =
let propertyChanged =
Event<PropertyChangedEventHandler,PropertyChangedEventArgs>()
let getPropertyName = function
| PropertyGet(_,pi,_) -> pi.Name
| _ -> invalidOp "Expecting property getter expression"
interface INotifyPropertyChanged with
[<CLIEvent>]
member this.PropertyChanged = propertyChanged.Publish
member private this.NotifyPropertyChanged propertyName =
propertyChanged.Trigger(this,PropertyChangedEventArgs(propertyName))
member this.NotifyPropertyChanged quotation =
quotation |> getPropertyName |> this.NotifyPropertyChanged
Aktualizacja:
Mam zmodyfikowano RelayCommand zdefiniowany w szablonie projektu MVVM dla F # na następujące kwestie:
module UILogic.Interaction
open System
open System.Windows
open System.Windows.Input
open System.ComponentModel
type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
let event = new DelegateEvent<EventHandler>()
interface ICommand with
[<CLIEvent>]
member this.CanExecuteChanged = event.Publish
member this.CanExecute arg = canExecute(arg)
member this.Execute arg = action(arg)
I wtedy aktualizowany mój ViewModel następująco:
module CreateProfile.UILogic
open UILogic.State
open UILogic.Interaction
type ViewModel() =
inherit ViewModelBase()
let mutable _firstName = ""
let mutable _lastName = ""
let mutable _submitted = false
member this.FirstName
with get() = _firstName
and set(value) =
_firstName <- value
base.NotifyPropertyChanged(<@ this.FirstName @>)
member this.LastName
with get() = _lastName
and set(value) =
_lastName <- value
base.NotifyPropertyChanged(<@ this.LastName @>)
member this.IsSubmitted
with get() = _submitted
and set(value) = _submitted <- value
member this.Submit =
new DelegateCommand ((fun _ -> this.IsSubmitted <- true), (fun _ -> true))
Jednak nie jestem pewien, jak wywołać funkcję wykonaj na Zatwierdź (DelegateCommand):
module CreateProfileViewModel.Tests
open FsUnit
open NUnit.Framework
open UILogic.State
open CreateProfile.UILogic
[<Test>]
let ``create profile``() =
// Setup
let viewModel = ViewModel()
viewModel.FirstName <- "Scott"
viewModel.LastName <- "Nimrod"
// Test
ignore viewModel.Submit.Execute(); // How to invoke execute?
// Verify
viewModel.IsSubmitted |> should equal true
Wiem, że to banalne, ale szczerze mówiąc nie mam pojęcia jak to zrobić.
Wszelkie sugestie?
Dlaczego nie używasz gotowych rozwiązań? - FSharp.ViewModule –
@ Foggy Finder: Naprawdę chcę zrozumieć i polecić F # w mojej praktyce programistycznej. Guy Coder stanowił doskonałe odniesienie. W tej chwili studiuję ich implementację, aby móc nią manipulować, gdy próbuję wchłonąć ten język i jego mechanikę. –
Jakie jest twoje pytanie? Gdzie masz problemy? –