F#中的DialogResult [英] DialogResult in F#
问题描述
尝试从MVVM应用程序的窗口中检索DialogResult时,我偶然发现了上一个问题一个>.实施建议的更改后,示例如下所示:
Trying to retrieve a DialogResult from a window in a MVVM app, I stumbled on this previous question. After implementing the suggested changes, the sample looks like:
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#中进行计算.
Now DialogResultChanged
is used before it is declared, which of course doesn't compute in F#.
我似乎找不到可行的解决方案,将不胜感激.
I can't seem to find a working solution, any help would be appreciated .
推荐答案
I'm not a WPF expert, but in this related C# solution, the DialogResultsChanged
method is static. If you define the method as static in F# too, you should be able to reference it before it is declared (using the full name DialogCloser.DialogResultsChanged
), so something like the following should do the trick:
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)
这篇关于F#中的DialogResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!