“视觉继承”的现状如何?

发布于 2024-07-05 01:31:41 字数 245 浏览 5 评论 0原文

我们有一个应用程序,它必须灵活地向用户显示其主表单 - 根据用户的不同,表单应该略有不同,也许这里或那里有一个额外的按钮,或者其他一些细微差别。 为了停止编写代码来显式删除或添加控件等,我转向视觉继承来解决问题 - 在我认为是一种整洁、干净和逻辑的 OO 风格 - 事实证明,一半的时间继承的表单都很难无缘无故地在 VS 中渲染它们自己等等 - 我感觉开发人员和微软在某种程度上回避了视觉继承的实践 - 你能证实这一点吗?我在这里遗漏了什么吗?

问候。

We have an application that has to be flexible in how it displays it's main form to the user - depending on the user, the form should be slightly different, maybe an extra button here or there, or some other nuance. In order to stop writing code to explicitly remove or add controls etc, I turned to visual inheritance to solve the problem - in what I thought was a neat, clean and logical OO style - turns out that half the time inherited forms have a hard time rendering themeselves in VS for no good reason etc - and I get the feeling that developers and to some extent Microsoft have shunned the practice of Visual Inheritance - can you confirm this, am I missing something here?

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

萌辣 2024-07-12 01:31:41

阅读此内容: http://cs.rthand.com /blogs/blog_with_righthand/archive/2005/11/10/186.aspx

AFAIK,视觉继承和依赖于设计元素集合的对象(通常是网格控件等)仍然存在问题。我相信微软仍然有删除了更改 f.ex 的可能性。 继承的表单/用户控件等中的 GridView。但其他控件(如 TextBox、Form、UserControl、Panel 等)应该按预期工作。

到目前为止,我自己使用第 3 方网格控件的 VI 没有任何问题,但你必须小心,特别是必须避免从集合中删除项目。

Read this: http://cs.rthand.com/blogs/blog_with_righthand/archive/2005/11/10/186.aspx

AFAIK, there are still issues with Visual Inheritance and objects that rely on collections for the design elements, typically grid controls etc. I believe MS still have removed the possibility of changing f.ex. a GridView in an inherited form/usercontrol etc. But other controls like TextBox, Form, UserControl, Panel etc. should work as expected.

I've so far had no problem with VI using 3rd party grid controls myself, but you have to be careful, in particular, removing items from collections MUST be avoided.

拥有 2024-07-12 01:31:41

我想我已经找到了避免这个问题的方法。

不要在父表单中挂钩 Form_Load 事件,这会破坏设计器。

也不要在父窗体中将默认的空构造函数从 Visual Studio 中删除。 如果您想要依赖注入,请创建另一个构造函数。

像这样:

public ProductDetail()
{
    InitializeComponent();
}

public ProductDetail(ISupplierController supplierController) : base()
{
    InitializeComponent();
    this.supplierController = supplierController;
}

然后,您仍然可以从继承的表单中执行此操作:

public NewProduct(ISupplierController supplierController)
    : base(supplierController)
{
    InitializeComponent();
}

到目前为止,这对我有用,而且我也遇到了一些奇怪的设计器问题。

干杯,丹尼尔

I think I've found a way how to avoid this problem.

Don't hook the Form_Load Event in your parent form, this will break the designer.

Also don't take the Default empty constructor away from Visual Studio in the Parent Form. If you want to have Dependency Injection, create another constructor.

Like this:

public ProductDetail()
{
    InitializeComponent();
}

public ProductDetail(ISupplierController supplierController) : base()
{
    InitializeComponent();
    this.supplierController = supplierController;
}

You can then still do this from your inherited Form:

public NewProduct(ISupplierController supplierController)
    : base(supplierController)
{
    InitializeComponent();
}

This worked for me so far, and I had some weird designer issues too.

cheers, Daniel

故乡的云 2024-07-12 01:31:41

我经常在 Visual Studio 中偶然发现此类问题。 在许多情况下,MSVS 表单设计器无法正确呈现表单。 在我使用 WinForms 的日子里,我必须使用各种奇怪的技巧来实现一些复杂的场景。 然而,我认为使用视觉继承是非常有益的,无论 MSVS 设计器存在什么错误,都不应该被丢弃。

I often stumble upon such problems in Visual Studio. In many cases MSVS forms designer fails to render form correctly. Back in the days I worked with WinForms I had to do all kind of weird tricks to enable some complex scenarios. However I think that using visual inheritance is very beneficial and should not be thrown away regardless of MSVS designer bugs.

纵情客 2024-07-12 01:31:41

我在 VS2005 中发现了一些问题。 它们主要是由于设计器中表单对象的构造问题造成的。 尝试从表单构造函数等访问数据库的代码存在问题。

您可以通过启动 Visual Studio 的第二个实例并在调试器中加载第一个实例来调试此类问题。 如果您在代码中设置断点,则可以在第一个实例中调试设计器中发生的情况。

我记得的另一个问题是表单类中的泛型,

public class MyForm<MyObject> : Form

这不起作用

I've seen some problems in VS2005 with this. They were mostly due to problems with construction of the forms-objects in the designer. There were issues with code that tried to access the database from the form-constructors etc.

You can debug issues like this by starting a second instance of visual studio and loading up the first instance in the debugger. If you set breakpoints in your code you can then debug what happens in the designers in the first instance.

Another problem I can remember was generics in form classes

public class MyForm<MyObject> : Form

this won't work

难以启齿的温柔 2024-07-12 01:31:41

我正在研究(诚然即将被淘汰的)MCAD,WinForms 元素的一部分是视觉继承。

我个人并没有遇到什么大问题,但是,有一些注意事项需要考虑

对我来说,主要问题始终是初始化。您需要记住,设计器不能/不以运行时的方式实例化表单(同样,它不能使用 Web dev 来执行此操作) ,这就是为什么需要小心自定义控件渲染)。

此外,一旦表单发生更改,就需要完全重新构建项目,以便将对表单的更改传播到继承自该表单的子表单。

我个人没有看到任何证据表明它已被“回避”。 据我所知,尽可能重用代码仍然是一种很好的做法。 视觉继承提供了这一点。

我可以建议使用示例代码根据您遇到的实际问题创建一个新问题吗? 然后我们可以查看它是否可以正常工作并解释原因:)

I am studying towards the (admittedly soon-to-be-obsoleted) MCAD, and part of the WinForms element was Visual Inheritence.

I personally have had no major problems with it, however, there are considerations to take in to account.

For me, the main problem has always initialization.. You need to remember that the designer cannot/does not instantiate forms in the same way it does at run time (similarly, it cannot do this with web dev, which is why care is needed with custom control rendering).

Also, once a form is changed, a complete re-build of the project is required in order to propagate the changes to the form to the child forms that inherit from it.

I personally have seen no evidence to suggest that it has been "shunned". AFAIK, its still good practice to exercise code re-use where possible. Visual inheritance provides that.

May I suggest creating a new question with the actual problems you are having, with sample code? We can then look at it to see if we can get it working and explain why :)

早乙女 2024-07-12 01:31:41

我认为他们在 2005 年已经或多或少地解决了桌面设计器的问题。
您尝试过常见的罪魁祸首吗?

  • 没有抽象控件类型
  • 没有任何形式的构造函数参数
  • 初始化移至 Form_Load 而不是 Ctor 与
  • 放置在其中的用户控件/表单位于同一项目中没有控件
  • 关闭所有文档 -> 干净-> 重建
  • 重新启动 VS

我似乎认为只要你做了以上所有的事情,它就有效了......大部分。

I thought they had more or less sorted the desktop designer issues in 2005.
Have you tried the usual culprits?

  • No abstract control types
  • No constructor arguments in any form
  • Initialisation moved to Form_Load as opposed to the Ctor
  • No controls in the same project as the usercontrol/form that they are put inside
  • Close all documents -> Clean -> Rebuild
  • Restart VS

I seemed to think that as long as you did all of the above it worked..... mostly.

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