有没有F#编写的ViewModel中调用DelegateCommand的示例?
我在这里找到了一个DelegateCommand实现的示例代码(链接):
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)
然而,作为一名对F#不太熟悉的人,我不确定是否应该使用这个实现方式,或者是否有其他方法可以利用。
以下是一个测试,一旦我弄清如何执行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
我的视图模型如下:
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 @>)
我有以下基本视图模型:
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
更新:
我已经修改了F# MVVM项目模板中定义的RelayCommand,修改后如下:
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)
我接下来更新了我的视图模型如下所示:
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))
然而,我不确定如何在提交(DelegateCommand)上调用Execute函数:
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
我知道这很琐碎,但是我真的不知道怎么做。
有什么建议吗?