如何以 MVP 模式将 UserControl 附加到表单?
我正在尝试使用 MVP 模式创建一种主/详细 UI。 我有常见的怀疑:
interface IMainView{}
class MainView: Form, IMainView{}
interface IMainPresenter{}
class MainPresenter{}
// Numerous domain objects
我还有一个 UserControl,它也是它自己的 MVP 三元组的视图:
interface ISubView{}
class SubView: UserControl, ISubView{}
interface ISubPresenter{}
class SubPresenter{}
MainPresenter 创建 SubPresenter 的实例,而 SubPresenter 又创建 SubView 的实例。 我的问题是视图不包含彼此的引用,甚至不知道彼此的存在。 他们只知道自己的演示者,但我想将一个作为 UserControl 的视图附加到另一个作为 Form 的视图。 这是否可能做到并且仍然保持每种观点对彼此的无知?
到目前为止,所有视图都已将每个演示者所需的属性公开为系统类型,因此如果 ListBox 更改为 ComboBox 或 RadioGroup,演示者不会受到影响。 如果可能的话我想保持这种模式,但如果我别无选择,我愿意打破这种模式。
我这样做的原因是 MainView 向用户展示了对象的集合。 每个对象可以是多个(超过 50 个)不同类之一。 所有对象都将实现一个通用接口,但操作每个对象的 UI 将随底层类的不同而变化。
顺便说一句,这是一个面向 .NET 2.0 的 Winforms 应用程序(尽管它被编译为 C# 3.0)
I'm trying to create a kind of master/detail UI using an MVP pattern. I have the usual suspects:
interface IMainView{}
class MainView: Form, IMainView{}
interface IMainPresenter{}
class MainPresenter{}
// Numerous domain objects
I also have a UserControl which is also a View of its own MVP triad:
interface ISubView{}
class SubView: UserControl, ISubView{}
interface ISubPresenter{}
class SubPresenter{}
The MainPresenter creates and instance of the SubPresenter, which in turn, creates an instance of SubView. My problem is the Views don't contain references to each other or even know each other exist. They only know about their own presenters but I want to attach one view that is a UserControl to another view that is a Form. Is this possible to do and still maintain each view's ignorance of each other?
Up until this point all the views have exposed the properties needed by each presenter as system types so the presenters would not be affected if a ListBox changed to a ComboBox or a RadioGroup. I'd like to keep it this way if possible but I'm willing to break this pattern if I have no other choice.
My reasons for doing this is the MainView presents the user with a collection of objects. Each object can be one of several (more than 50) different classes. All will implement a common interface but the UI for manipulating each object will vary with the underlying class.
By the way, this is a Winforms application targeting .NET 2.0 (it's compiled as C# 3.0 though)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过让副演示者将对其视图的引用传递给主演示者来解决这个问题,然后主演示者将其传递给其视图,然后将其分配给一个空面板。
它作为普通的旧对象传递,因此两个演示者都不需要包含对 winforms 命名空间的引用。 mainView 简单地假设它是 UserControl 的后代,并将其强制转换为 UserControl。
I solved this by having the subpresenter pass a reference to its view to the main presenter which then passes it to its view, which then assigns it to an empty panel.
It's passed as a plain old object so neither of the presenters need to include references to the winforms namespace. The mainView simply assumes its a decedent of UserControl and casts it as such.