简体   繁体   English

通用表单和VS设计器

[英]Generic forms and VS designer

I have a base class 我有一个基类

internal partial class View<T> : UserControl
  where T : class
{
    protected T t;
}

and I want to derive a child from View 我想从View派生一个孩子

internal partial class ViewChild<T> : View<T>
  where T : class
{
}

it works OK, but I cannot edit ViewChild in the VS designer. 它工作正常,但我无法在VS设计器中编辑ViewChild。 I know the problem is generic base class. 我知道问题是通用基类。 However I do not understand how I can avoid that in this case. 但是我不明白在这种情况下我怎么能避免这种情况。 Is there any way to fix that? 有没有办法解决这个问题?

There is another way, and it doesn't rely on compiler flags: 还有另一种方法,它不依赖于编译器标志:

http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/ http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/

I really wouldn't advise the use of conditional compilation. 我真的不建议使用条件编译。 Much better to work with the framework, and not against it. 使用框架更好,而不是反对它。

Basically, you can give VS a different class through the existing framework. 基本上,您可以通过现有框架为VS提供不同的类。 You decorate your base class with a TypeDescriptionProvider attribute which tells VS to use a different class as a designer. 您使用TypeDescriptionProvider属性装饰您的基类,该属性告诉VS使用不同的类作为设计器。

As mentioned in the original blog post, there may be caveats associated with this workaround, but I got it working neatly on a project with > 25 UserControls inheriting from a common base class. 正如原始博客文章中所提到的,可能存在与此变通方法相关的警告,但我在一个项目上工作得很好,其中> 25个UserControl继承自公共基类。

Generics break the designer because it cannot instantiate the class without a type T . 泛型破坏了设计者,因为它不能在没有类型T情况下实例化类。 I explain a workaround in my blog post: 我在我的博客文章中解释了一个解决方法:

http://adamhouldsworth.blogspot.co.uk/2010/02/winforms-visual-inheritance-limitations.html http://adamhouldsworth.blogspot.co.uk/2010/02/winforms-visual-inheritance-limitations.html

In short, you need to "resolve" the type with an intermediary class: 简而言之,您需要使用中间类“解析”类型:

  • BaseControl<T> : UserControl
  • CustomerControl_Design : BaseControl<Customer>
  • CustomerControl : CustomerControl_Design

You can then conditionally switch this class out of the code based on the DEBUG or RELEASE compiler switches: 然后,您可以根据DEBUGRELEASE编译器开关有条件地将此类从代码中切换出来:

#if DEBUG

namespace MyNamespace
{
    using System;


    public partial class CustomerEditorControl_Design : BaseEditorControl<Customer>
    {
        public CustomerEditorControl_Design()
            : base()
        {
            InitializeComponent();
        }
    }
}

#endif

    public partial class CustomerEditorControl
#if DEBUG
        : CustomerEditorControl_Design
#else
        : BaseEditorControl<Customer>
#endif
    {
    }

This will let you open the derived class of CustomerControl , unfortunately you will never be able to design a UI control with generics in the signature. 这将允许您打开CustomerControl的派生类,遗憾的是,您永远无法在签名中设计带有泛型的UI控件。 My solution is only enabling the design of derived items. 我的解决方案只是启用派生项的设计。

I have no idea why CustomerControl : BaseControl<Customer> won't work as in this case the type T is defined, but it simply doesn't - I'm guessing because of the rules of generic usage. 我不知道为什么CustomerControl : BaseControl<Customer>将无法工作,因为在这种情况下定义了类型T ,但它根本没有 - 我猜测是因为通用的规则。

To their defense, Microsoft do say that this isn't supported. 为了他们的辩护,微软确实说这不受支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用自定义 TypeDescriptionProvider 仍然是在 VS2012 设计器中使用通用表单的唯一方法吗? - Is using a custom TypeDescriptionProvider still the only way around using generic forms in the designer for VS2012? 后代通用表单无法在表单设计器中显示! - Descendant generic forms cannot display in the form designer! 在VS 2010中可视化类设计器中的泛型继承的类型参数 - Visualize type arguments of generic inheritance in class designer in VS 2010 你能改变VS Win Forms设计器代码生成吗? - Can you change VS Win Forms designer code generation? 为什么具有通用类型的基本Windows Forms表单类会停止设计器的加载? - Why do base Windows Forms form class with generic types stop the designer loading? Windows窗体,设计器和单例 - Windows Forms, Designer, and Singleton Visual Studio形成设计师 - Visual Studio forms designer Forms Designer中的组件数组 - Array of Components in Forms Designer 使用框架 4.7.2 运行 SDK 项目的 vs2019 中的控件和表单缺少图标和视图设计器选项 - controls and forms missing icon and view designer option in vs2019 running SDK project with framework 4.7.2 适用于 .NET Core 的 Windows 窗体设计器和 WPF 设计器 - Windows Forms Designer and WPF Designer for .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM