Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 无法将ViewModelBase和ViewModel划分为不同的文件_F# - Fatal编程技术网

F# 无法将ViewModelBase和ViewModel划分为不同的文件

F# 无法将ViewModelBase和ViewModel划分为不同的文件,f#,F#,我有一个WPF项目,正在努力找出如何拥有两个独立的文件: ViewModelBase 视图模型 当我将类放在它们自己的文件中时,我收到viewModel的编译错误,因为找不到ViewModelBase 此外,我正在使用WPF项目。因此,我无法根据文件依赖关系上下移动解决方案资源管理器中的文件 代码如下: module MVVM open System.ComponentModel open Microsoft.FSharp.Quotations.Patterns type Vie

我有一个WPF项目,正在努力找出如何拥有两个独立的文件:

  • ViewModelBase
  • 视图模型
当我将类放在它们自己的文件中时,我收到viewModel的编译错误,因为找不到ViewModelBase

此外,我正在使用WPF项目。因此,我无法根据文件依赖关系上下移动解决方案资源管理器中的文件

代码如下:

module MVVM

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


    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 @>)

        member this.GetFullName() = 
            sprintf "%s %s" (this.FirstName) (this.LastName)
模块MVVM
开放系统组件模型
打开Microsoft.FSharp.quotes.Patterns
类型ViewModelBase()=
让propertyChanged=
事件()
让getPropertyName=函数
|PropertyGet(3;,pi,3;)->pi.Name
|->invalidOp“应为属性getter表达式”
接口INotifyProperty更改为
[]
成员this.PropertyChanged=PropertyChanged.Publish
成员private this.NotifyPropertyChanged propertyName=
触发器(此,PropertyChangedEventArgs(propertyName))
成员this.NotifyPropertyChanged报价单=
QUOTE |>getPropertyName |>this.NotifyPropertyChanged
类型ViewModel()=
继承ViewModelBase()
让可变名字=“”
让可变lastName=“”
请记住这个名字
使用get()=firstName
和设置(值)=

firstName将视图模型保存在单独的项目中,与XAML定义的视图保持一致,这是一个很好的主意。这样,您就可以积极地在视图和视图模型之间实施分离。如果将视图模型保留在传统的F#项目中,则可以像平常一样对文件进行重新排序。好的。那是因为。只是想做一个快速而肮脏的hello world应用程序。为什么它是一个WPF项目会阻止你重新排序文件?如果它与VS有关,则始终可以通过手动编辑.proj文件来完成。