有什么问题:
let (x:UIElement) = upcast new TextBlock()
错误是:此处需要类型“System.ComponentModel.ISupportInitialize”,但该类型不可用。您必须添加对程序集“System, Version=4.0.0....”的引用
TextBlock
是 UIElement
的子类型...
请注意,按照错误消息所说的操作确实可以解决问题,但为什么有必要执行像向上转换这样基本的操作?
最佳答案
正如 lasseespeholt 在他的(现已删除?)回答中提到的,您的代码没有任何问题,您只需添加对 System.dll
的引用作为错误信息提示。
但是这是怎么回事?
您在该特定行收到错误消息,因为它是编译器遇到 System.dll
库(一个接口(interface) ISupportInitialize
,它是由 TextBlock
实现)并意识到它需要对库的引用才能理解类型。
获得相同错误信息的另一种方法是这样写:
let x = new TextBlock()
x. // If you get IntelliSense here, you'll see just '<Note>'
// with the same error message as the one you're getting
在这种情况下,IntelliSense 需要查看类型(以便它可以填充成员完成)。
关于.net - F# 将 TextBlock 向上转换为 UIElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3537874/