为什么 VB.NET 编译器会认为接口未实现(而实际上它已实现)?

发布于 2024-08-28 06:42:18 字数 2040 浏览 6 评论 0原文

更新

我认为当我最初发布这个问题时我还不够清楚。

看看这些屏幕截图。

替代文本链接到此处的更大屏幕截图

请注意我用红色框起来的部分。此处显示的类确实实现了INotifyPropertyChanged,但VB编译器似乎认为声明的PropertyChanged事件与的签名不匹配>INotifyPropertyChanged.PropertyChanged

替代文本链接到此处的更大屏幕截图

在这里,我选择了有问题的行代码。在这个和下一个屏幕截图之间,我实际上只是剪切并粘贴完全相同的行回到文件中(即,我按下Ctrl + X 后跟 Ctrl + V)。

替代文本链接到此处的更大屏幕截图

现在看吧!剪切并粘贴该线后,错误就消失了。

这里发生了什么?


原始问题

我有时会发生这种情况,特别是根据我的经验,使用 INotifyPropertyChanged 接口,但我不知道问题是否仅限于该单一接口(这看起来很奇怪)或不。

假设我有一些像这样设置的代码。有一个包含单个事件的接口。一个类实现该接口。它包括事件。

Public Interface INotifyPropertyChanged
    Event PropertyChanged As PropertyChangedEventHandler
End Interface

Public Class Person
    Implements INotifyPropertyChanged

    Public Event PropertyChanged _
    (ByVal sender As Object, ByVal e As PropertyChangedEventArgs) _
        Implements INotifyPropertyChanged.PropertyChanged

    ' more code below '
End Class

有时,当我构建项目时,编译器会突然开始表现得像上面的代码已损坏。它将报告 Person 类未实现 INotifyPropertyChanged,因为它没有 PropertyChanged 事件;或者它会说 PropertyChanged 事件无法实现 INotifyPropertyChanged.PropertyChanged 因为它们的签名不匹配。

这确实很奇怪,但最奇怪的部分是:如果我只是剪掉以 Event PropertyChanged 开头的行,然后将其粘贴回去,错误就会消失。 项目构建。

有人知道这里会发生什么吗?

Update

I don't think I was clear enough when I originally posted this quesion.

Take a look at these screenshots.

alt text
(Link to bigger screenshot here)

Notice the portions I've boxed in red. The class displayed here does implement INotifyPropertyChanged, but the VB compiler seems to think that the PropertyChanged event as declared does not match the signature of INotifyPropertyChanged.PropertyChanged.

alt text
(Link to bigger screenshot here)

Here I've selected the offending line of code. Between this and the next screenshot I literally just cut and paste the exact same line back into the file (i.e., I hit Ctrl + X followed by Ctrl + V).

alt text
(Link to bigger screenshot here)

Now behold! After cutting and pasting the line back in, the error goes away.

What is going on here?


Original question

I have this happen sometimes, particularly with the INotifyPropertyChanged interface in my experience but I have no idea if the problem is limited to that single interface (which would seem bizarre) or not.

Let's say I have some code set up like this. There's an interface with a single event. A class implements that interface. It includes the event.

Public Interface INotifyPropertyChanged
    Event PropertyChanged As PropertyChangedEventHandler
End Interface

Public Class Person
    Implements INotifyPropertyChanged

    Public Event PropertyChanged _
    (ByVal sender As Object, ByVal e As PropertyChangedEventArgs) _
        Implements INotifyPropertyChanged.PropertyChanged

    ' more code below '
End Class

Every now and then, when I build my project, the compiler will suddenly start acting like the above code is broken. It will report that the Person class does not implement INotifyPropertyChanged because it doesn't have a PropertyChanged event; or it will say the PropertyChanged event can't implement INotifyPropertyChanged.PropertyChanged because their signatures don't match.

This is weird enough as it is, but here's the weirdest part: if I just cut out the line starting with Event PropertyChanged and then paste it back in, the error goes away. The project builds.

Does anybody have any clue what could be going on here?

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

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

发布评论

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

评论(2

一世旳自豪 2024-09-04 06:42:18

该代码对我来说工作正常(Visual Studio 2008),您一定遇到了一些错误。

不管怎样,你也可以这样实现:

Public Class Person
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

End Class

The code works fine for me (Visual Studio 2008), you must be encountering some bug.

Anyhow, you can also implement it this way:

Public Class Person
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

End Class
彩虹直至黑白 2024-09-04 06:42:18

您需要将事件标记为公开

You need to mark the event Public

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