F# 中的对话框结果

DialogResult in F#

试图从 MVVM 应用程序中的 window 检索 DialogResult,我偶然发现了 this previous question。实施建议的更改后,示例如下所示:

type DialogCloser() =

    static let DialogResultProperty =
        DependencyProperty.RegisterAttached("DialogResult", typeof<bool>, typeof<DialogCloser>, new PropertyMetadata(DialogResultChanged))

    static member GetDialogResult (a:DependencyObject) =
        a.GetValue(DialogResultProperty) :?> bool

    static member SetDialogResult (a:DependencyObject) (value:string) = 
        a.SetValue(DialogResultProperty, value)

    member this.DialogResultChanged (a:DependencyObject) (e:DependencyPropertyChangedEventArgs) =
        let window = a :?> Window
        match window with
        | null -> failwith "Not a Window"
        | _ -> window.DialogResult <- System.Nullable (e.NewValue :?> bool)

现在 DialogResultChanged 在声明之前使用,这当然不会在 F# 中计算。

我似乎找不到可行的解决方案,我们将不胜感激。

我不是 WPF 专家,但在 this related C# solution 中,DialogResultsChanged 方法是静态的。如果您也在 F# 中将方法定义为静态方法,您应该能够在声明它之前引用它(使用全名 DialogCloser.DialogResultsChanged),因此像下面这样的东西应该可以解决问题:

type DialogCloser() =

    static let DialogResultProperty =
        DependencyProperty.RegisterAttached
            ( "DialogResult", typeof<bool>, typeof<DialogCloser>, 
              new PropertyMetadata(DialogCloser.DialogResultChanged))

    static member GetDialogResult (a:DependencyObject) =
        a.GetValue(DialogResultProperty) :?> bool

    static member SetDialogResult (a:DependencyObject) (value:string) = 
        a.SetValue(DialogResultProperty, value)

    static member DialogResultChanged 
            (a:DependencyObject) (e:DependencyPropertyChangedEventArgs) =
        let window = a :?> Window
        match window with
        | null -> failwith "Not a Window"
        | _ -> window.DialogResult <- System.Nullable (e.NewValue :?> bool)