使用 Windows 窗体作为抽象类 - 使用哪种模式?
我正在一次又一次地遇到这种情况,但我不确定我做事的方式是否错误,或者我是否可以以不同的方式做事。
一个例子:
我有一个 Windows 窗体,它有一个 DataGridView 和一些私有方法来执行数据网格的验证并解释在 datagridview 上的鼠标右键单击等。这个窗口窗体本质上是一个“抽象”类,永远不会直接实例化。
然后,我从这个基类继承并以各种方式(模板模式)自定义它,例如定义 datagridview 的列和特定于这些列的特定格式化方法等。
当我使用这些类时,基类公共方法形成我的接口和我可以实例化我想要的特定类型的 datagridview 并通过通用接口对其进行操作。 迷人的。
问题:
主要问题是,您实际上无法将 Windows 窗体类声明为抽象类,而不会导致 Visual Studio 设计器抛出错误,因为它无法实例化这些抽象类。
一些解决方案:
目前,我正在基类中“实现”我想要覆盖的那些方法:
throw new NotSupportedException();
所以至少如果我忘记覆盖形成我的接口的这些方法之一。 不过,这对我来说似乎很臭,而且我不太喜欢它。
我尝试过的另一个解决方案是完全取消继承并定义一个接口(例如 IMyDataGrid)并在每个 datagridview 类中实现该接口(某种策略模式)。 但这里的问题是,您失去了代码重用的好处,继承意味着您必须创建许多不同的表单,在它们上面放置一个 datagridview - 有效地将相同的代码复制并粘贴到每个表单中。 坏的。
有没有更好的方法来实现这一目标?
I'm struggling with a situation that I come up again time and time again but I am not sure whether the way that I am doing things is wrong or whether I could be doing things in a different way.
An Example:
I have a Windows Form that has a DataGridView with some private methods to perform validation of the datagrid and interpreting right mouse clicks on the datagridview etc. This windows form is essentially an "abstract" class and is never instantiated directly.
I then inherit from this base class and customise it in various ways (template pattern) e.g. define the columns of the datagridview and particular formatting methods which are specific to those columns etc.
When I consume these classes the base class public methods form my interface and I can instantiate the particular type of datagridview that I want and manipulate it through the common interface. Lovely.
The Problem:
The main problem is that you cannot actually declare a Windows Form class as abstract without causing the Visual Studio designer to throw a wobbly as it cannot instantiate these abstract classes.
Some Solutions:
At the moment I am "implementing" those methods in the base class that I want to be overridden with:
throw new NotSupportedException();
so at least if I forget to override one of these methods that form my interface. This seems rather smelly to me though and I don't really like it.
Another solution I toyed with was to do away with inheritance altogether and define an interface (e.g. IMyDataGrid) and implement that in each datagridview class (sort of strategy pattern). The problem here though is that you lose the benefits of code re-use that inheritance gives you meaning that you have to create many different forms, drop a datagridview on them - effectively copy and pasting the same code into each one. Bad.
Is there a better way of trying to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的要求,有多种方法可以做到这一点。
DataGridView
派生类virtual
方法而不是使用abstract
类您必须选择一个最适合您需求的选项。 在不了解您提出问题的领域和具体情况的情况下,我认为我们无法给您 100% 确定的答案。
There are many ways to do this depending on your requirements.
DataGridView
that implements an interface to do the custom logicvirtual
methods instead of using anabstract
classYou'll have to pick an option that best suits your needs. Without knowing the domain and specifics in which your question is being asked, I don't think we can give you a 100% certain answer.
查看此方法了解如何创建所需的两个属性。
您需要以下属性和类型描述符类(代码取自 UrbanPotato),
将它们分配给您的抽象形式,如下所示:
创建一个将从您的抽象形式继承的新类。 这个类将由 Visual Studio 实例化,
这应该可以工作。
Check out this method to know how to create the two attributes needed.
You need the following attribute and type descriptor class (code taken from UrbanPotato)
Assign them to your abstract form like this:
Create a new class that will inherit from your abstract form. This class will be instanciated by Visual Studio
This should work.
我也有同样的问题。
此页面可能会帮助您,即使它只是一个解决方法: 从摘要继承表单类(并使其在设计器中工作)。
我还没有找到更好的解决方案,但似乎没有。 因此,Windows 窗体设计器确实迫使您调整类设计。
I have the same problem.
This page may help you, even though it is just a workaround: Inheriting a Form from an Abstract Class (and Making it Work in the Designer).
I haven't found any better solution, but it seems there isn't. So indeed the Windows Forms Designer is forcing you to adapt your class design.