我有两个功能,水平和垂直,用于布置控件。他们是这样工作的:
let verticalList = vertical [new TextBlock(Text = "one");
new TextBlock(Text = "two");
new TextBlock(Text = "three")]
现在verticalList是一个垂直显示三个文本块的控件:
one
two
three
以下是定义:
let horizontal controls =
let wrap = new WrapPanel() in
List.iter (wrap.Children.Add >> ignore) controls ;
wrap
let vertical controls =
let stack = new StackPanel() in
List.iter (stack.Children.Add >> ignore) controls ;
stack
当我组合不同类型时出现问题:
let foo = vertical [new TextBlock(Text = "Title"); vertical items]
这抱怨列表的元素不是同一类型。这是真的,但它们有一个共同的超类型(UIElement)。
我知道我可以使用 :> UIElement 向上转换列表中的两个项目,但这是一个丑陋的解决方案。F# 可以推断出常见的超类型。如果不是,为什么不呢?
如果好看就太好了
vertical [X; Y; Z]
不必成为
vertical [(X :> UIElement); (Y :> UIElement); (Z :> UIElement)]