- (void)didMoveToParentViewController:(UIViewController *)parent
{
if (![parent isEqual:self.parentViewController]) {
NSLog(@"Popup close!");
}
}
当我像上面那样上课时,尝试这样的事情。
type AdderType() =
/// Appends to the container.
static member (+)
(cont:DockPanel,child:#UIElement) =
cont.Children.Add child |> ignore
child
我收到错误let dock = DockPanel()
let win = Window(Title = "Check the Window Style", Content = dock)
let menu = dock + Menu()
我受到启发,通过Phil Trelford's绑定示例制作上述内容,如下所示:
None of the types 'DockPanel,Menu' support the operator '+'.
以上由于某种原因起作用。我不知道为什么。是否可以重载type DependencyPropertyValuePair(dp:DependencyProperty,value:obj) =
member this.Property = dp
member this.Value = value
static member (+)
(target:#UIElement,pair:DependencyPropertyValuePair) =
target.SetValue(pair.Property,pair.Value)
target
或其他一些操作符,以便我可以优雅地将控件添加到容器中?
答案 0 :(得分:7)
在类中定义的运算符仅在其中一个参数是类的实例时才起作用,但您可以将运算符定义为全局运算符:
let (++) (cont:DockPanel) (child:#UIElement) =
cont.Children.Add child |> ignore
child
然后应该有以下工作:
let dock = DockPanel()
let win = Window(Title = "Check the Window Style", Content = dock)
let menu = dock ++ Menu()
但说实话,我不认为这种问题是使用自定义运营商的好地方。在这里使用+
会让人感到困惑,因为在任何意义上你都不是添加两件事。您的运营商不是可交换的,例如(a ++ b) <> (b ++ a)
。
我认为一个更惯用的代码是定义一个命名函数并使用|>
:
let appendTo (cont:DockPanel) (child:#UIElement) =
cont.Children.Add child |> ignore
child
let dock = DockPanel()
let win = Window(Title = "Check the Window Style", Content = dock)
let menu = Menu() |> appendTo dock
答案 1 :(得分:2)
与上一个例子类似的方法是这样的:
type ContainerType(con:Panel) =
member this.Children = con.Children
static member (+)
(child:#UIElement,cont:ContainerType) =
cont.Children.Add child |> ignore
child
let toCon = ContainerType
实际上我更喜欢托马斯的解决方案。 Phil Trelford的例子看起来非常像.NET的一部分让我想到static member (+)
中的一个参数有DependencyPropertyValuePair
类型,这就是为什么{ {1}}运算符可能首先被重载。这就是答案。