F# 将 TextBlock 向上转换为 UIElement

发布于 2024-09-15 00:55:59 字数 303 浏览 6 评论 0原文

问题出在哪里:

let (x:UIElement) = upcast new TextBlock()

错误是:此处需要类型“System.ComponentModel.ISupportInitialize”,但不可用。您必须添加对程序集“System,Version=4.0.0....”的引用

TextBlock UIElement 的子类型...

请注意,按照错误消息所述执行操作确实可以解决问题,但为什么需要执行向上转换这样的基本操作呢?

What is wrong with:

let (x:UIElement) = upcast new TextBlock()

The error is: The type 'System.ComponentModel.ISupportInitialize' is required here and is unavailable. You must add a reference to assembly 'System, Version=4.0.0....'

TextBlock is a subtype of UIElement...

Note that doing what the error message says does solve the issue, but why is that necessary to do something as basic as upcasting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-09-22 00:55:59

正如lasseespeholt在他的(现已删除?)答案中提到的,您的代码没有任何问题,您只需按照错误消息的提示添加对System.dll的引用。

但是发生了什么事?
您在该特定行上收到错误消息,因为它是编译器首先遇到 System.dll 库中某种类型的地方(接口 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 需要查看类型(以便它可以填充成员完成)。

As lasseespeholt mentioned in his (now deleted?) answer, there is nothing wrong with your code and you just need to add the reference to System.dll as the error message suggests.

But what is going on?
You are getting the error message on that particular line, because it is the first place where the compiler encounters some type from the System.dll library (an interface ISupportInitialize, which is implemented by TextBlock) and realizes that it needs the reference to the library in order to understand the type.

Another way to get the same error message is to write this:

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

In this case, the IntelliSense needs to look at the type (so that it can populate member completion).

没企图 2024-09-22 00:55:59

来自文档

“在许多面向对象的语言中,向上转换是隐式;在 F# 中,当您将参数传递给对象类型的方法时,会自动应用向上转换,但是,对于模块中的 let 绑定函数,向上转换不是自动的,除非将参数类型声明为 a。有关详细信息,请参阅灵活类型 (F#)。 ”

如果您使用以下语法:

let (x:#UIElement) = new TextBlock()

您的代码将使用灵活类型(由 # 指示)并且可以编译。但是,现在您会收到警告:

“此构造导致代码不如其类型注释所指示的通用。在 'c' 处或附近使用 '#'、'_' 或其他类型注释所隐含的类型变量:\path\Program.fs' 已被限制为“TextBlock”类型。”

From the documentation:

"In many object-oriented languages, upcasting is implicit; in F#, the rules are slightly different. Upcasting is applied automatically when you pass arguments to methods on an object type. However, for let-bound functions in a module, upcasting is not automatic, unless the parameter type is declared as a flexible type. For more information, see Flexible Types (F#)."

If you'd use the following syntax:

let (x:#UIElement) = new TextBlock()

your code would use a flexible type (indicated by the #) and it would compile. However, now you would get a warning:

"This construct causes code to be less generic than indicated by its type annotations. The type variable implied by the use of a '#', '_' or other type annotation at or near 'c:\path\Program.fs' has been constrained to be type 'TextBlock'."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文