C#:从多个类扩展

发布于 2024-09-27 18:18:44 字数 613 浏览 0 评论 0原文

假设我有一个 ViewModel TabViewModel,它扩展了 MVVM 基础框架中 ViewModel 的 ObservableObject 类。然后我还有一个扩展 TabViewModelEditorTabViewModel,现在我需要扩展 DependencyObject 来实现 DependencyProperties。我不能延长超过一堂课。我该如何实现这个?我可以上一门“中级”课程,比如……

TabViewModel : ObservableObject

EditorTabViewModel : TabViewModel

DependentEditorTabViewModel : DependencyObject

但那是一门额外的不必要的课程。有更好的方法来做到这一点吗?

更新

实际上我无法执行上述操作。 DependentEditorTabViewModel 仍然需要扩展 EditorTabViewModel ...除了 DependencyObject

Suppose I have a ViewModel TabViewModel that Extends ObservableObject the class for ViewModels in the MVVM Foundation Framework. Then I also have a EditorTabViewModel that extends TabViewModel, Now I need to extend DependencyObject to implement DependencyProperties. I cannot extend more than 1 class. How might I implement this? I could have an "intermediate" class like ...

TabViewModel : ObservableObject

EditorTabViewModel : TabViewModel

DependentEditorTabViewModel : DependencyObject

but thats 1 extra unnecessary class. Any better way to do this?

UPDATE

Ops actually I cant do the above. DependentEditorTabViewModel still need to extend EditorTabViewModel ... apart from DependencyObject

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

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

发布评论

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

评论(2

小巷里的女流氓 2024-10-04 18:18:44

C# 不支持多重继承。最好的选择是使用接口,而不是父类。

即使您无法选择使用接口(也许您无权访问代码),通常最好 更喜欢组合而不是继承。您真的需要继承这两个类,还是可以组合它们?

C# does not support Multiple Inheritance. Your best bet is to use Interfaces, rather than parent classes.

Even if you don't have the option of using interfaces (maybe you don't have access to the code), it's generally better to prefer composition over inheritance. Do you really need to inherit both of these classes, or can you compose with them instead?

忘年祭陌 2024-10-04 18:18:44

如果您完成了所需的任务,那么这不是一个额外的课程。您将按照以下方式进行操作:

DependentEditorTabViewModel : DependencyObject

TabViewModel : DependentEditorTabViewModel

Its not an extra class if you're accomplishing what you need. Here is how you would go about that:

DependentEditorTabViewModel : DependencyObject

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