使用 Windows 窗体作为抽象类 - 使用哪种模式?

发布于 2024-08-02 00:26:02 字数 823 浏览 5 评论 0原文

我正在一次又一次地遇到这种情况,但我不确定我做事的方式是否错误,或者我是否可以以不同的方式做事。

一个例子:

我有一个 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 技术交流群。

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

发布评论

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

评论(3

你的他你的她 2024-08-09 00:26:02

根据您的要求,有多种方法可以做到这一点。

  • 将表单内容放入实现接口以执行自定义逻辑的用户控件中
  • 从实现接口以执行自定义逻辑的DataGridView派生类
  • 如前所述,使用具体的使用virtual 方法而不是使用abstract
  • ...

您必须选择一个最适合您需求的选项。 在不了解您提出问题的领域和具体情况的情况下,我认为我们无法给您 100% 确定的答案。

There are many ways to do this depending on your requirements.

  • Put the form content into a user control that implements an interface to do the custom logic
  • Derive classes from DataGridView that implements an interface to do the custom logic
  • As mentioned, use a concrete class with virtual methods instead of using an abstract class
  • ...

You'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.

那请放手 2024-08-09 00:26:02

查看此方法了解如何创建所需的两个属性。

您需要以下属性和类型描述符类(代码取自 UrbanPotato),

// Source : taken from http://www.urbanpotato.net/default.aspx/document/2001 Seem to be down
// Allow the designer to load abstract forms
namespace YourNamespace
{

    // Place this attribute on any abstract class where you want to declare
    // a concrete version of that class at design time.
    [AttributeUsage(AttributeTargets.Class)]
    public class ConcreteClassAttribute : Attribute
    {
        Type _concreteType;
        public ConcreteClassAttribute(Type concreteType)
        {
            _concreteType = concreteType;
        }

        public Type ConcreteType { get { return _concreteType; } }
    }

    // Here is our type description provider.  This is the same provider
    // as ConcreteClassProvider except that it uses the ConcreteClassAttribute
    // to find the concrete class.
    public class GeneralConcreteClassProvider : TypeDescriptionProvider
    {
        Type _abstractType;
        Type _concreteType;

        public GeneralConcreteClassProvider() : base(TypeDescriptor.GetProvider(typeof(Form))) { }

        // This method locates the abstract and concrete
        // types we should be returning.
        private void EnsureTypes(Type objectType)
        {
            if (_abstractType == null)
            {
                Type searchType = objectType;
                while (_abstractType == null && searchType != null && searchType != typeof(Object))
                {

                    foreach (ConcreteClassAttribute cca in searchType.GetCustomAttributes(typeof(ConcreteClassAttribute), false))
                    {
                        _abstractType = searchType;
                        _concreteType = cca.ConcreteType;
                        break;
                    }
                    searchType = searchType.BaseType;
                }

                if (_abstractType == null)
                {
                    // If this happens, it means that someone added
                    // this provider to a class but did not add
                    // a ConcreteTypeAttribute
                    throw new InvalidOperationException(string.Format("No ConcreteClassAttribute was found on {0} or any of its subtypes.", objectType));
                }
            }
        }

        // Tell anyone who reflects on us that the concrete form is the
        // form to reflect against, not the abstract form. This way, the
        // designer does not see an abstract class.
        public override Type GetReflectionType(Type objectType, object instance)
        {
            EnsureTypes(objectType);
            if (objectType == _abstractType)
            {
                return _concreteType;
            }
            return base.GetReflectionType(objectType, instance);
        }


        // If the designer tries to create an instance of AbstractForm, we override 
        // it here to create a concerete form instead.
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            EnsureTypes(objectType);
            if (objectType == _abstractType)
            {
                objectType = _concreteType;
            }

            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
}

将它们分配给您的抽象形式,如下所示:

[TypeDescriptionProvider(typeof(GeneralConcreteClassProvider))]
[ConcreteClass(typeof(MyAbstractConcreteForm))]
public abstract partial class MyAbstractForm : Form
{
}

创建一个将从您的抽象形式继承的新类。 这个类将由 Visual Studio 实例化,

public class MyAbstractConcreteForm: MyAbstractForm 
{
    public MyAbstractConcreteForm() : base() { }
}

这应该可以工作。

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)

// Source : taken from http://www.urbanpotato.net/default.aspx/document/2001 Seem to be down
// Allow the designer to load abstract forms
namespace YourNamespace
{

    // Place this attribute on any abstract class where you want to declare
    // a concrete version of that class at design time.
    [AttributeUsage(AttributeTargets.Class)]
    public class ConcreteClassAttribute : Attribute
    {
        Type _concreteType;
        public ConcreteClassAttribute(Type concreteType)
        {
            _concreteType = concreteType;
        }

        public Type ConcreteType { get { return _concreteType; } }
    }

    // Here is our type description provider.  This is the same provider
    // as ConcreteClassProvider except that it uses the ConcreteClassAttribute
    // to find the concrete class.
    public class GeneralConcreteClassProvider : TypeDescriptionProvider
    {
        Type _abstractType;
        Type _concreteType;

        public GeneralConcreteClassProvider() : base(TypeDescriptor.GetProvider(typeof(Form))) { }

        // This method locates the abstract and concrete
        // types we should be returning.
        private void EnsureTypes(Type objectType)
        {
            if (_abstractType == null)
            {
                Type searchType = objectType;
                while (_abstractType == null && searchType != null && searchType != typeof(Object))
                {

                    foreach (ConcreteClassAttribute cca in searchType.GetCustomAttributes(typeof(ConcreteClassAttribute), false))
                    {
                        _abstractType = searchType;
                        _concreteType = cca.ConcreteType;
                        break;
                    }
                    searchType = searchType.BaseType;
                }

                if (_abstractType == null)
                {
                    // If this happens, it means that someone added
                    // this provider to a class but did not add
                    // a ConcreteTypeAttribute
                    throw new InvalidOperationException(string.Format("No ConcreteClassAttribute was found on {0} or any of its subtypes.", objectType));
                }
            }
        }

        // Tell anyone who reflects on us that the concrete form is the
        // form to reflect against, not the abstract form. This way, the
        // designer does not see an abstract class.
        public override Type GetReflectionType(Type objectType, object instance)
        {
            EnsureTypes(objectType);
            if (objectType == _abstractType)
            {
                return _concreteType;
            }
            return base.GetReflectionType(objectType, instance);
        }


        // If the designer tries to create an instance of AbstractForm, we override 
        // it here to create a concerete form instead.
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            EnsureTypes(objectType);
            if (objectType == _abstractType)
            {
                objectType = _concreteType;
            }

            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
}

Assign them to your abstract form like this:

[TypeDescriptionProvider(typeof(GeneralConcreteClassProvider))]
[ConcreteClass(typeof(MyAbstractConcreteForm))]
public abstract partial class MyAbstractForm : Form
{
}

Create a new class that will inherit from your abstract form. This class will be instanciated by Visual Studio

public class MyAbstractConcreteForm: MyAbstractForm 
{
    public MyAbstractConcreteForm() : base() { }
}

This should work.

所谓喜欢 2024-08-09 00:26:02

我也有同样的问题。
此页面可能会帮助您,即使它只是一个解决方法: 从摘要继承表单类(并使其在设计器中工作)

我还没有找到更好的解决方案,但似乎没有。 因此,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.

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