.net - F# 将 TextBlock 向上转换为 UIElement

标签 .net f# uielement

有什么问题:

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/

相关文章:

c# - 在 C# 中获取 List<T> 中不同值的列表

f# - 何时评估模块中的 F# `do` 语句?

visual-studio - 我可以在 F# 中创建跨平台 Xamarin Forms 应用程序吗?

c# - 使用 C# 从 UIElement 获取 WPF 屏幕截图 JPG

c# - 防止 ScrollViewer 处理鼠标滚轮事件

c# - 使用 MethodCallExpression 的表达式调用 "Any"

c# - Windows Forms 和 Aero 不能很好地结合——如何使它们看起来像原生的?

.net - 注入(inject)staple命令时postscript编码不一致

F# WSDL 类型提供程序和 Confluence

ios - iOS 用户界面元素的名称