Visual Studio 2008 Winform设计器无法加载继承自泛型类的窗体。

12

我有一个WinForms项目,并且我创建了一个类放在A程序集中,它继承自System.Windows.Forms.Form以作为我的项目中各种窗体的基类。这个基类大致如下:

public partial class DataForm<T> : Form where T : class
{

    T currentRecord;

    protected T CurrentRecord
    {
        get
        {
            return currentRecord;
        }
        set
        {
            currentRecord = value;
            CurrentRecordChanged();
        }
    }
}

现在,当我在程序集 B 上创建一个继承自我的 DataForm 的表单时,设计器无法加载,但如果我编译它,应用程序可以正常运行。

该表单如下:

public partial class SomeTableWindow : DataForm<SomeTable>
{
    public SomeTableWindow ()
    {
        InitializeComponent();
    }
}

我遇到的错误是:

The designer could not be shown for this file because none of the classes within it can be designed. 
The designer inspected the following classes in the file: CultivosWindow --- The base
class 'TripleH.Erp.Controls.DataForm' could not be loaded. Ensure the assembly has 
been referenced and that all projects have been built.    


Instances of this error (1)  

1.   Hide Call Stack 

at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.EnsureDocument(IDesignerSerializationManager manager)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)  

这是设计器上的一个错误吗?还是我做错了什么?有没有什么解决方法?

谢谢你的帮助。

1个回答

16

这是已知的限制。基本上,您可以通过声明另一个从泛型类继承的类来解决此问题。

例如:

class Generic<T> : UserControl
{
}

然后

class GenericInt : Generic<int> { }

那么使用GenericInt代替Generic。我知道很糟糕。


我可以确认这种解决方法行得通。而且,无论你将中间类(例如GenericInt)放在程序集A还是程序集B中,它都能工作。 - Ryan Lundy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接