2

我在 F# 中有以下 ViewModelBase,我正在尝试构建它以使用 WPF 学习 F#。

module MVVM

open System
open System.Collections.ObjectModel
open System.ComponentModel
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
open System.Reactive.Linq


module Property =

    let ToName(query : Expr) = 
        match query with
        | PropertyGet(a, b, list) ->
            b.Name
        | _ -> ""

    let SetValue<'t>(obj, query : Expr<'t>, value : 't) =
        match query with
        | PropertyGet(a, b, list) ->
            b.SetValue(obj, value)
        | _ -> ()

    let GetValue<'o, 't>(obj : 'o , query : Expr<'t>) : option<'t> =
        match query with
        | PropertyGet(a, b, list) ->
            option.Some(b.GetValue(obj) :?> 't )
        | _ -> option.None

    let Observe<'t>(x: INotifyPropertyChanged) (p : Expr<'t>)  =
        let name = ToName(p)
        x.PropertyChanged.
            Where(fun (v:PropertyChangedEventArgs) -> v.PropertyName = name).
            Select(fun v -> GetValue(x, p).Value)

type ViewModelBase() =
    let propertyChanged = new Event<_, _>()

    interface INotifyPropertyChanged with
        [<CLIEvent>]
        member x.PropertyChanged = propertyChanged.Publish

    abstract member OnPropertyChanged: string -> unit

    default x.OnPropertyChanged(propertyName : string) =
        propertyChanged.Trigger(x, new PropertyChangedEventArgs(propertyName))

    member x.SetValue<'t>(expr : Expr<'t>, v : 't) =
        Property.SetValue(x, expr, v)
        x.OnPropertyChanged(expr)

    member x.OnPropertyChanged<'t>(expr : Expr<'t>) =
        let propName = Property.ToName(expr)
        x.OnPropertyChanged(propName)

但是我从编译器得到一个错误

Error   1   The type 'ViewModelBase' is used in an invalid way. 
A value prior to 'ViewModelBase' has an inferred type involving 
'ViewModelBase', which is an invalid forward reference. 

然而,编译器并没有告诉我之前的值是问题的违规部分。由于我对 F# 使用的类型推断非常陌生,因此我可能遗漏了一个明显的问题。

仅供参考,该代码旨在像下面一样使用,但目前该代码已被注释掉,错误仅与上面的核心代码有关

type TestModel() as this = 
    inherit MVVM.ViewModelBase()

    let mutable name  = "hello"

    let subscription = (Property.Observe this  <@ this.SelectedItem @>).
            Subscribe(fun v -> Console.WriteLine "Yo")

    member x.SelectedItem 
        with get() = name
        and set(v) = 
            x.SetValue(<@ x.SelectedItem @>, v)
4

1 回答 1

6

我找到了。

let SetValue<'t>(obj, query : Expr<'t>, value : 't) =
    match query with
    | PropertyGet(a, b, list) ->
        b.SetValue(obj, value)
    | _ -> ()

受到约束。应该

let SetValue<'t>(obj : Object, query : Expr<'t>, value : 't) =
    match query with
    | PropertyGet(a, b, list) ->
        b.SetValue(obj, value)
    | _ -> ()
于 2012-12-21T06:34:29.360 回答