F#中的DialogResult

时间:2015-10-21 11:06:39

标签: wpf f# dialogresult

尝试从MVVM应用程序中的窗口检索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#中计算。

我似乎无法找到合适的解决方案,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:3)

我不是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)