是否有关于如何在用F#编写的viewmodel上调用DelegateCommand的示例?
我找到了以下CodeateCommand实现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)
然而,作为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
我的viewmodel如下:
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)
然后我更新了我的viewmodel,如下所示:
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))
但是,我不知道如何在Submit (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
我知道这是微不足道的,但老实说,我不知道如何做到这一点。
有什么建议吗?
答案 0 :(得分:3)
将Submit
实施更改为:
member this.Submit =
DelegateCommand ((fun _ -> this.IsSubmitted <- true), (fun _ -> true)) :> ICommand
并将其称为:
viewModel.Submit.Execute() |> ignore
重要的变化是Submit
方法的返回类型现在是ICommand
而不是DelegateCommand
。这是由于使用了向上运算符:>
。
在F#中,所有界面都明确实施。这意味着虽然DelegateCommand
实现了ICommand
,但ICommand
的方法在声明(或推断)为DelegateCommand
的对象上不可见。当声明(或推断)值为ICommand
类型时,另一方面,只有ICommand
的成员可用。
答案 1 :(得分:2)
由于我没有在F#中使用MVC或MVVM,因此无法给出具体的代码示例。
有关
然而,作为F#的新手,我不确定是否应该使用它 实施或是否有其他方法我应该 杠杆。
如果您使用的Visual Studio也可以免费下载community edition,那么MVC和MVVM都有模板。
使用菜单从VS访问模板。
档案 - &gt;新 - &gt;项目
在线扩展
展开模板
展开Visual F#
在&#34;搜索在线模板的右上方&#34;输入一个术语,如MVC或MVVM
选择一个或检查几个。
注意:有些模板已经存在了几年,而且F#和Windows在此过程中取得了很大进展,因此我倾向于避免使用较旧的模板。此外,流行的作者通常可以在网上找到,甚至有些人都在这里,所以请询问有关细节的问题。
您还可以使用互联网浏览器在Visual Studio Gallery查看VS的其他模板,控件和扩展程序。
警告:某些扩展程序会在安装时出现问题,尤其是较旧或较不受欢迎的扩展程序,因此请小心并且不要将它们安装在生产计算机上,直到您对其进行测试为止先是另一台机器。